Add more than one append to the angular http header

Asked

Viewed 59 times

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);
    }
  }

}
  • 1

    Truly a mystery! The only point I can imagine can be with the return of the localStorage may be coming different from null.

  • 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?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.