Angular 5 JWT always says ‘invalid token’

I made a common Interceptor for punching in the Authorization token in header for every request. The token is saved from Spring Boot backend properly into session storage. The same token works in Postman but in Angular services it fails(when calling REST APIs from Spring Boot). In the browser console it shows HTTP 500 invalid token Any help will be appreciated.

JwtInterceptor.ts

import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class JwtInterceptor {

    constructor() {}

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        // tslint:disable-next-line:prefer-const
        const token = sessionStorage.getItem('token');
        if (token) {
            request = request.clone({
                headers: request.headers.set('Authorization', 'Bearer ' + token)
            });
        }
        return next.handle(request);
    }

}

Source: Angular

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.