How to keep authentication token?

Viewed 166

The React website has following requirements:

  1. Stay logged in when opening a page of the current website in new tab.
  2. Log out if browser is closed and reopened.
  3. Stay logged in on page refresh.
  4. IE11 support :(.

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?

1 Answers
Related