Skip to content

Instantly share code, notes, and snippets.

@vadamk
Created October 9, 2018 14:48
Show Gist options
  • Save vadamk/acc30c10a248ffd845fe94047e0bb7f9 to your computer and use it in GitHub Desktop.
Save vadamk/acc30c10a248ffd845fe94047e0bb7f9 to your computer and use it in GitHub Desktop.
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