Skip to main content
removeu 7 caracteres do conteúdo
Fonte Link
Dante
  • 24
  • 2
    login(username: string, password: string, fingerprint: string, admin: boolean) {
        return this.http.post<any>("http://site.com/url/login", { username, password, admin, fingerprint}, httpOptions)
                .pipe(map(response => {
                  let user = {token: response.headers.authorizationAuthorization}
                  // login successful if there's a jwt token in the response
                  console.log("USER: " + user);
    
                    if (user) {
                        // store user details and jwt token in local storage to keep user logged in between page refreshes
                        localStorage.setItem('currentUser', JSON.stringify(user));
                        this.currentUserSubject.next(user);
                    }
    
                    return user;
                }));
        }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = this.authenticationService.currentUserValue;

        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: {
                  'Authorization': `Bearer $`${currentUser.token}`,
                  'Accept-Language': '1'
                }
            });
        }

        return next.handle(request);
    }
    login(username: string, password: string, fingerprint: string, admin: boolean) {
        return this.http.post<any>("http://site.com/url/login", { username, password, admin, fingerprint}, httpOptions)
                .pipe(map(response => {
                  let user = {token: response.headers.authorization}
                  // login successful if there's a jwt token in the response
                  console.log("USER: " + user);
    
                    if (user) {
                        // store user details and jwt token in local storage to keep user logged in between page refreshes
                        localStorage.setItem('currentUser', JSON.stringify(user));
                        this.currentUserSubject.next(user);
                    }
    
                    return user;
                }));
        }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = this.authenticationService.currentUserValue;

        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: {
                  'Authorization': `Bearer ${currentUser.token}`,
                  'Accept-Language': '1'
                }
            });
        }

        return next.handle(request);
    }
    login(username: string, password: string, fingerprint: string, admin: boolean) {
        return this.http.post<any>("http://site.com/url/login", { username, password, admin, fingerprint}, httpOptions)
                .pipe(map(response => {
                  let user = {token: response.headers.Authorization}
                  // login successful if there's a jwt token in the response
                  console.log("USER: " + user);
    
                    if (user) {
                        // store user details and jwt token in local storage to keep user logged in between page refreshes
                        localStorage.setItem('currentUser', JSON.stringify(user));
                        this.currentUserSubject.next(user);
                    }
    
                    return user;
                }));
        }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = this.authenticationService.currentUserValue;

        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: {
                  'Authorization': `${currentUser.token}`,
                  'Accept-Language': '1'
                }
            });
        }

        return next.handle(request);
    }
Fonte Link
Dante
  • 24
  • 2

Pelo que entendi, seu problema é conseguir acessar seu token JWT na resposta da sua chamada POST de login, certo?

Neste caso, para que funcione da maneira que espera, a implementação teria que ser semelhante a esta que coloquei abaixo.

O ts de login:

this.authenticationService.login(this.f.username.value, this.f.password.value, this.fingerprint, this.admin)
      .subscribe(
        user => {
          this.router.navigate(['/']);
        },
        error => {
          this.error = error;
          this.loading = false;
        });
  }

Cabeçalho:

   const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json;charset=UTF-8',
        'Accept-Language': '1'
      }),
      observe: 'response'
    };

Service de login:

    login(username: string, password: string, fingerprint: string, admin: boolean) {
        return this.http.post<any>("http://site.com/url/login", { username, password, admin, fingerprint}, httpOptions)
                .pipe(map(response => {
                  let user = {token: response.headers.authorization}
                  // login successful if there's a jwt token in the response
                  console.log("USER: " + user);
    
                    if (user) {
                        // store user details and jwt token in local storage to keep user logged in between page refreshes
                        localStorage.setItem('currentUser', JSON.stringify(user));
                        this.currentUserSubject.next(user);
                    }
    
                    return user;
                }));
        }

Interceptador:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = this.authenticationService.currentUserValue;

        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: {
                  'Authorization': `Bearer ${currentUser.token}`,
                  'Accept-Language': '1'
                }
            });
        }

        return next.handle(request);
    }