When I log in, I store the access token on session storage and try to access it in other components using the oninit() method. However, I keep getting a null value. Upon checking the console, I can see that the token is being stored in the session.
Here is how I set the session storage during a post HttpClient request:
.subscribe(
(data: any) => { // json data
console.log('Success: ', data);
sessionStorage.setItem("Token ",data.access_token)
this._router.navigate( ['dashboard'] );
},
And here is how I am trying to access the token in another component:
ngOnInit() {
let url:string = '.......';
const token = sessionStorage.getItem("Token");
console.log(token);
this.http.get(url,
{
headers : new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization' :'Bearer '+ token
})
}
).subscribe(
(data: any)=> { // json data
console.log('Success: ', data.data);
this.leads = data.data;
},
error => {
console.log('Error: ', error);
}
)
}
}