Created
October 9, 2018 14:48
-
-
Save vadamk/acc30c10a248ffd845fe94047e0bb7f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
@Injectable() | |
export class JwtInterceptor implements HttpInterceptor { | |
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
// add authorization header with jwt token if available | |
const userSession = JSON.parse(localStorage.getItem('userSession')); | |
if (userSession && userSession.auth_token) { | |
request = request.clone({ | |
setHeaders: { | |
Authorization: `Bearer ${userSession.auth_token}` | |
} | |
}); | |
} | |
return next.handle(request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment