I have a token
stored in localStorage
, I have also a method that verifies if the token is correct or not, as you can see in this code:
let token = localStorage.getItem('token');
console.log(token);
if ( !token )
{
return null;
}
const parts = token.split('.');
if ( parts.length !== 3 )
{
throw new Error('error');
}
....
if the token is undefined
I will return null, else continue the verification.
There are two issues I can't understand:
- Why did I lose the token from the localStorage when I refresh the page
- Why does the
if ( !token )
return false whentoken
isundefined
, the first console.log return undefined, but my method continues toconst parts = token.split('.');
.