Recover Header from an Http Response with angular

1

I need to retrieve the Token that comes in the authorization and save it in the localstorage, but I'm not getting it.

My login method within my service is:

public login(email: string, password: string): Observable<User> {
        return this.http.post<User>(this.url, { username: email, password: password })
            .do(user => {
                this.user = user;
            });
    }

I believe this information I can recover from subscriber:

public login(): void {
        this.signinService.login(this.loginForm.value.email, this.loginForm.value.password).subscribe(
            user => {
                this.notificationService.notify('Usuário logado com sucesso');
            },
            (response: HttpErrorResponse) => this.notificationService.notify(response.error.message),
            () => {
                this.router.navigate([atob(this.navigateTo)]);
            });
    }
    
asked by anonymous 09.12.2018 / 16:19

1 answer

0

You can read the header return like this:

http
  .get<any>('url', {observe: 'response'})
  .subscribe(resp => {
    console.log(resp.headers.get('authorization'));
  }); 
    
11.12.2018 / 04:33