I'm new to react and I wanna know how to store a string like a JSON Web Token in the browser after user logs in...?
And also how to remove when user logs out?
I'm new to react and I wanna know how to store a string like a JSON Web Token in the browser after user logs in...?
And also how to remove when user logs out?
When the component mounts or in the constructor, you can check to see if the user's browser has this data already using localstorage.getItem('webToken')
and/or set it using localstorage.setItem('webToken', token)
. To clear it on logout you could remove it by using localstorage.removeItem('webToken')
.
It should be noted that users may not click log out, but instead just leave or close the page. This means it is possible that the local storage item will persist on the next page load. Since local storage does not expire, this could pose potential issues.