Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis
Created August 18, 2018 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdyrrahitis/9441bf83b0ea193246f69462ea5b6b2d to your computer and use it in GitHub Desktop.
Save gdyrrahitis/9441bf83b0ea193246f69462ea5b6b2d to your computer and use it in GitHub Desktop.
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (this.authService.isAuthenticated()) {
const accessToken = JSON.parse(this.authService.getToken()).access_token;
const token = `Bearer ${accessToken}`;
const authReq = req.clone({ headers: req.headers.set("Authorization", token) });
return next.handle(authReq) as any;
}
return next.handle(req) as any;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment