I am making an interceptor that should add in my header a token and an authorization whenever the value of authToken is different from null. My problem is in how to add an append in my header.
I tried something like:
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor() {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
const authToken = localStorage.getItem('token');
if(authToken != null){
const newRequest = req.clone({
setHeaders: { Authorization: 'bearer ${authToken}' }
});
return next.handle(newRequest);
}else{
const newRequest = req.clone({});
return next.handle(newRequest);
}
}
}