I made jwt authorization and I stored that token into session storage , but when i try to get that token into string and log it i get null , this is the function
login(user : User): void
{
this.header= new Headers({"content-Type": "application/json"});
this.osobaService.create(user,this.tokenUrl,this.header).then(p=> {sessionStorage.setItem("Token",p._body.slice(1,-1))});
this.tokenString = sessionStorage.getItem("Token");
console.log(this.tokenString);
}
It should be simple getItem method but I'm getting null .
The 'this.osobaService.create' is a async function. You are trying to log a value, before writing it. If you change your code like this, this should log a correct value.
login(user : User): void
{
this.header= new Headers({"content-Type": "application/json"});
this.osobaService.create(user,this.tokenUrl,this.header)
.then(p=> {
sessionStorage.setItem("Token",p._body.slice(1,-1))
this.tokenString = sessionStorage.getItem("Token");
console.log(this.tokenString);
});
}