5

I have added an authorization header via an HttpInterceptor in Angular, but the output doesn't seem correct. Based on the JSON output after the header has been added this feels far too unwieldy to be correct Am I adding headers incorrectly, or am I accessing them wrong?:

headers["lazyUpdate"].forEach(x=>{x.name=='Authorization'}).value

Goal is to return Bearer mystring

interceptor called:

{"url":"https://myurl","body:{}, "reportProgress":false, "withCredentials":false, "responseType":"json", "method":"POST", "headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"params":{"updates":null,"cloneFrom":null,"encoder":{},"map":null},"urlWithParams":"myurl"}

token.interceptor.ts:13 interceptor added token:

{"url":"myurl", "body":{}, "reportProgress":false, "withCredentials":false, "responseType":"json", "method":"POST", "headers":{"normalizedNames":{},"lazyUpdate":[{"name":"Authorization","value":"Bearer mystring","op":"s"}], "headers":{},"lazyInit":{"normalizedNames":{},"lazyUpdate":null,"headers":{}}},"params":{"updates":null,"cloneFrom":null,"encoder":{},"map":{}},"urlWithParams":"myurl"}

My TokenInterceptor class:

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {   
    console.log('interceptor called: '+JSON.stringify(request))

    let r = request.clone({
      headers: request.headers.set('Authorization', `Bearer ${localStorage.getItem("id_token")}`)
    });
    console.log('interceptor added token: '+JSON.stringify(r))
    return next.handle(r);
  }
}
Rilcon42
  • 9,584
  • 18
  • 83
  • 167

0 Answers0