I am making a global authorization header in my app. I've used the interceptor so i won't declare the authorization header in my get() functions. Am i correctly implementing the interceptor since when i call the get() functions, it still asking for the token. It says token is not provided. Is there a problem in my auth.interceptor? Should i declare the authorization header bearer token in every get() functions? I thought the interceptor is called every time there's a request sent/receive?
auth.interceptor.ts
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
private authService: AuthService;
constructor(private injector: Injector) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Get the auth header from the service.
this.authService = this.injector.get(AuthService);
const token = this.authService.getToken();
if (token) {
req = req.clone({ headers: req.headers.set('Authorization', 'Bearer ' + token) });
}
if (!req.headers.has('Content-Type')) {
req = req.clone({ headers: req.headers.set('Content-Type', 'application/json') });
}
req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
return next.handle(req);
}
}
products.component.ts
getAll() {
return this.httpClient.get<any>(this.url).map(response => response.json());
}