Reputation: 605
I have this issue with angular JWT token, which works on my api from Postman but not from the angular call. Request details as follow:
I copied the token showed in the response and generate a request through Postman which works.
Any idea why this token could be invalid when sent from angular service?
Postman result below:
here is the code that produces the token angular side>
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor} from '@angular/common/http';
//import { AuthService } from './auth/auth.service';
import { Observable } from 'rxjs/Observable';
import { Http } from "@angular/http/http";
import { AuthService } from "./auth.service";
import { HttpClient } from "@angular/common/http";
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});
return next.handle(request);
}
}
public getToken(): string {
return localStorage.getItem('token');
}
Upvotes: 1
Views: 2740
Reputation: 18939
The authorization header is usually on this format:
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
It looks like you're trying to send an entire object. Send only the token field instead.
Upvotes: 5