Skip to content

Instantly share code, notes, and snippets.

@pankajparkar
Last active May 4, 2020 11:40
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 pankajparkar/ba0bb6a30338da81b157cf898f8fd50d to your computer and use it in GitHub Desktop.
Save pankajparkar/ba0bb6a30338da81b157cf898f8fd50d to your computer and use it in GitHub Desktop.
token.interceptor.ts
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth/auth.service';
import { Observable } from 'rxjs';
// You can add URL which don't need AUTH header
const whiteListUrls = ['login', 'refreshToken']
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (whiteListUrls.find(w => request.url.includes(w))) {
return next.handle(request);
}
const token = window.localStorage.get('token');
request = request.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
return next.handle(request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment