2020-12-30

Getting 401 Error while fetching list from API after implementing JWT access token in Angular

In my service .ts file i coded like this

getUsers(): Observable<any> {
    let httpOptions = new HttpHeaders().set('Authorization', 'Bearer ' + 
    this.securityService.securityObject.bearerToken);
    return this.http.get(environment.baseApiUrl + "user", { headers: httpOptions });
}

In the bearerToken we have token value, but the headers showing in the httpOptions is like below image. (took from the console)

enter image description here

in my controller class i have

    [HttpGet]
    [Authorize]
    public async Task<IActionResult> Get()
    {

In my HttpInterceptor i have written like

  var token = localStorage.getItem("bearerToken");

  if (token) {
  const newReq = request.clone(
    {
      headers: request.headers
        .set('Authorization',
          'Bearer ' + token)
    });

  return next.handle(newReq);
}
else {
  return next.handle(request);
}

my local storage have my token value, but

When i debug the code what i can see is its not entering inside the clone is this is the reason for headers not any entries like my image. Is clone ok to use there?



from Recent Questions - Stack Overflow https://ift.tt/2KJF4Pg
https://ift.tt/3mXZd1c

No comments:

Post a Comment