Last active
March 29, 2020 15:03
-
-
Save pankajparkar/3b7ae96f96f3471bf96e940297c68a0f 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'; | |
@Injectable() | |
export class AuthInterceptor implements HttpInterceptor { | |
// AuthService is holding | |
constructor(public auth: AuthService) {} | |
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
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