Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis

gdyrrahitis/auth-interceptor.ts Secret

Created Aug 18, 2018
Embed
What would you like to do?
@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
You can’t perform that action at this time.