Last active
May 4, 2020 11:40
-
-
Save pankajparkar/ba0bb6a30338da81b157cf898f8fd50d to your computer and use it in GitHub Desktop.
token.interceptor.ts
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 { 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