How to get the Header of a response with Angular 2+?

1

How do I get a Header for a response with Angular 2+? In my case, I'm using version 6.

I would like to get this authorization data to be stored in the Browser's LocalSession.

I am using the following method to perform the request:

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

            });
    }

In my component class, try to redeem through the HttpHeaderResponse Method, but without success!

public login(): void {
        this.signinService.login(this.loginForm.value.email, this.loginForm.value.password
        ).subscribe(
            (user: User) => {
                this.notificationService.notify('Usuário logado com sucesso');
                let header: HttpHeaderResponse = new HttpHeaderResponse();
                console.log(header);
                localStorage.setItem('token', 'Aqui vai o Token');
            },
            (response: HttpErrorResponse) => this.notificationService.notify(response.error.message),
            () => {
                this.router.navigate([atob(this.navigateTo)]);
            });
    }

Thank you for the collaboration of the community!

    
asked by anonymous 22.10.2018 / 15:52

1 answer

0

You could do something like this:

  public login(email: string, password: string): Observable<User> {
      return this.http
                 .post<User>(this.url, { username: email, password: password })
                  .pipe(
                    tap( resp => {
                      const token = resp.headers.get('X-Access-Token');
                      localStorage.setItem('token', token);
                      this.user = user;
                    }));
  }
    
23.10.2018 / 17:02