question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatic silent refresh fails with multiple tabs when localStorage is used

See original GitHub issue

Describe the bug When an application configures the angular-oauth2-oidc client to use localStorage, the automatic silent refresh process for the code flow fails when multiple tabs are opened.

Stackblitz example The issue can be trivially reproduced using the sample app, with the only required modification being to add the following to appModule.ts

export function storageFactory(): OAuthStorage {
  return localStorage;
}

and add the following provider in the module:

{ provide: OAuthStorage, useFactory: storageFactory },

To Reproduce Steps to reproduce the behavior:

  1. Open the sample app in a new incognito tab in Chrome
  2. Click “Login with Code Flow” and sign in
  3. Open dev tools and observe the periodic console messages saying that the refresh token is being used
  4. Open the sample app in a new incognito tab
  5. Open dev tools in the new tab
  6. Check the consoles in both tabs, eventually an error will appear in one of them
  7. Observe error in the console: Error refreshing token

Note that the session checking kicks in and causes the token to refresh again successfully. In applications where session checking is not configured, the refreshing does not recover.

Expected behavior The refreshing of tokens should be thread safe such that when localStorage is used multiple tabs do not try to refresh using the same refresh token at the same time.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:9

github_iconTop GitHub Comments

3reactions
myushchenkocommented, Jul 1, 2021

In order to fix issue with localStorage I suggest to use new Lock API https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API and refresh token by hand

locks.request('my_resource', async () => {
  await this.oAuthService.refreshToken();
});
1reaction
jeroenheijmanscommented, Aug 16, 2020

new incognito tab in Chrome … The error Message is than allways “login_required”

@KirschbaumP This is I think a different symptom than this issue describes (a race condition), and most likely due to third party cookie problems? I presume your IDS lives on another domain than your SPA when you encounter this? You could check my blogpost https://infi.nl/nieuws/spa-necromancy/ for a problem description, and some potential solutions/workarounds.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Have angular-oauth2-oidc retrieve access token from other tabs
First up: I somehow got the idea that sessionStorage was right for tokens and that localStorage should always be avoided.
Read more >
Authentication State Persistence | Firebase - Google
A developer may want to allow different users to sign in to an application on different tabs. The default behavior is to persist...
Read more >
Use Refresh Token Rotation - Auth0
This helps users to silently migrate to using Refresh Tokens without making them log in again. If the exchange fails because useRefreshTokens is...
Read more >
LocalStorage, sessionStorage - The Modern JavaScript Tutorial
Shared between all tabs and windows from the same origin. The data does not expire. It remains after the browser restart and even...
Read more >
My battle with browser tabs. Simple beginnings - Medium
I created a simple universal auto-refresh package, ... not support local storage across multiple tabs. localsync will automatically fallback ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found