0
I’m making a Interceptor
which should add to my header a token and an Authorization whenever the value of authToken
is different from null.
My problem is how to add a 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);
}
}
}
Truly a mystery! The only point I can imagine can be with the return of the localStorage may be coming different from null.
– codermarcos
There doesn’t seem to be anything wrong with the code, I would recommend just capitalizing the bearer => Bearer, otherwise there’s a good chance the api will respond with 401, because it’s case sensitive. You would know if Token is at least being passed in the network tab of the console?
– Henrique Assunção