Reputation: 58
The React website has following requirements:
If I keep auth token in sessionStorage, it meets the 2 requirement but not the 1.
If I use localStorage - 1 but not 2.
If I use:
window.onunload = () => {
localStorage.removeItem('authtoken');
};
it meets 1 and 2 conditions but it also clears storage when I refresh the page which shouldn't happen too!
This solution to check if the page is refreshing can't be used in IE11 https://stackoverflow.com/a/53307588/12994741
So how should I keep the auth token? Any way to make it? Maybe it's possible to prevent unload event on refreshing in React?
Upvotes: 2
Views: 338
Reputation: 155
You can use session cookies.
It keeps its value when you open a same website in different tabs.
They are expired once the browser exits. https://stackoverflow.com/a/8894280/15881471
It keeps its value on page refresh.
It works for IE11, though there are some caveats. https://github.com/cmp-cc/vue-cookies/issues/29
Upvotes: 1