I have login page in my app with token base authorization. In this app I have component called sessions. To view sessions I have to give access token.
here is my login.component.ts
this.authService.login(this.formGroup.value).subscribe(results => {
const tokens = JSON.parse(results.tokens.replace(/'/g,'"'));
localStorage.setItem('userTokens',tokens.access);
console.log(tokens.access);
this._router.navigate(['/sessions'])
}
This is my authService.ts where I have pass token to the header.
getSessions(){
return this.http.get('http://localhost:8000/api/sessions/all/'
,{headers : new HttpHeaders({'Authorization' : 'JWT ' + localStorage.getItem('userTokens')})});
}
This is my home.component.ts
this.authService.getSessions().subscribe((data : any )=>{
this.Sessions = data;
});
But this gives me unauthorized error. I tried same token in postman. It works finely in postman. Can someone help me for this.
question from:https://stackoverflow.com/questions/66060545/angular-authorization-with-jwt-token