How can I just get the token out of my local storage?

const token = localStorage.getItem(‘currentUser’);
console.log('Token: ',token)

{"token":"eyJ0eXAiOiefwefwewfwefiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9jaG9vc2FwaS50ZXN0XC9hcGlcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNjE4MjY5NTc2LCJleHAiOj344HDFMTgzNTU5NzYsIm5iZiI6MTYxODI2OTU3NiwianRfewfwfqS2kzQlA4ZU5qMCIsInN1YiI6MSwicHJ2IjoiMjNiZDVjODk0OWY2MDBhZGIzOWU3MDFjNDAwODcyZGI3YTU5NzZmNyJ9.nQYwcSHP6cXzwlgpv9APEj8HXEZs2NCXqeq3uUwOiWU","token_type":"bearer","token_validity":86400,"authdata":"am9obi5tYmlkZHVscGhAZ21haWwuY29tOlNjYXJhbWFuZ2E3OTM1IQ=="}```

Just take the string and parse it through JSON using its “parse” method and then you can reference the token as a property.

const token = localStorage.getItem('currentUser');
let token_obj = JSON.parse(token); //<--- Take string from storage and parse it to an object.
console.log(token_obj.token); //<---- Token will be a member of the new object.

Hope this works for you. :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.