1

I have a code for the authentication service in my application developed in angular 7. I need to recover the data sent in the headers of the API, but these arrive empty.

This is the autheticationService:

import {Injectable} from '@angular/core';
import {HttpClient, HttpResponse, HttpHeaders} from '@angular/common/http';
import {BehaviorSubject, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {User} from '../../models/index';
import {global} from '../global';

@Injectable({providedIn: 'root'})

export class AuthenticationService {
    private currentUserSubject: BehaviorSubject<User>;
    public currentUser: Observable<User>;

    constructor(private http: HttpClient) {
        this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
        this.currentUser = this.currentUserSubject.asObservable();
    }

    public get currentUserValue(): User {
        return this.currentUserSubject.value;
    }

    login(email: string, password: string): Observable<HttpResponse<any>> {
        return this.http.post(`${global.URL}/user/login`, {email, password}, {observe: 'response'});
    }

    logout() {
        localStorage.removeItem('currentUser');
        this.currentUserSubject.next(null);
    }
}

This is the login-component

import {Component, OnInit} from '@angular/core';
import {FormControl, FormGroup, Validators, FormBuilder} from '@angular/forms';
import {User} from '../../models/index';
import {Router, ActivatedRoute} from '@angular/router';
import {AuthenticationService} from '../../services/index';
import {first} from 'rxjs/operators';


@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss']
})

export class LoginComponent implements OnInit {

    loginForm: FormGroup;
    model: User;

    constructor(private route: ActivatedRoute, private router: Router, formBuilder: FormBuilder, private authenticationService: AuthenticationService) {
        this.model = new User();
        this.loginForm = formBuilder.group({
        email: new FormControl(this.model.email, [Validators.required, Validators.email]),
        password: new FormControl(this.model.password, [Validators.required]),
        });
    }


    ngOnInit() {
        this.authenticationService.logout();
        this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
    }

    submitForm() {
        if (this.loginForm.invalid) {
            return;
        }
        this.authenticationService.login(this.form.email.value, this.form.password.value)
            .subscribe(
                data=>{
                    console.log(data);
                },
                error=>{
                    console.log(error)
                }
             }

    }
}

The response is the following

enter image description here

And this is the headers

enter image description here

I need to recover the token, but this does not come.

Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110
Dasaaf
  • 145
  • 2
  • 11

0 Answers0