To avoid cyclic dependency i am using the Injector
to inject the AuthService
but when i run the application Angular execute the intercept()
method before setting the authService
property !!!
@Injectable()
export class TokenInterceptorService implements HttpInterceptor{
private authService;
constructor(private injector: Injector) {
setTimeout(() => {
this.authService = injector.get(AuthService);
console.log('===========================================',this.authService);
});
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.authService.getToken()}`
}
});
return next.handle(request);
}
}