Assuming that all the tabs run on the same browser and therefore can share cookie storage, the following mechanism can be implemented:
- Upon session start, every tab obtains it's own
unique_tab_id
. - The first tab is assigned as a
tab_in_charge
, who will dispatch everything there is to do regarding authentication. - When another tab needs to refresh the token, the tab sends a request to the
tab_in_charge
via a cookie or local storage. - The
tab_in_charge
has the obligation to check everyX
seconds if arefresh request
has been submitted by another tab. - If there is more than one request to refresh the token, then it only accepts the first and rejects the other, as when the new access token is received, then all of the tabs will receive that token.
Another thing I would add here is Proactive Token Refreshing
. You can use the iat
and exp
data from the token payload to calculate when it will expire, therefore giving you info about how much time you have. You can make it request a new access token 45 seconds
before it expires, and adding this information in the request you send to the tab_in_charge
. This will ensure resilient communication with your sockets, making sure there is zero down time while waiting for a new token or restarting the connection.
Finally, you need to gracefully transfer the position of tab_in_charge
to another tab if you are closing the current.