oidc-client-ts icon indicating copy to clipboard operation
oidc-client-ts copied to clipboard

Network error after waking up from sleep

Open CanD42 opened this issue 1 year ago • 4 comments

We see in Chrome after PC woke up from sleep:

POST /oauth2/token net::ERR_INTERNET_DISCONNECTED [JsonService] postForm: Network error [SilentRenewService] _tokenExpiring: Error from signinSilent: TypeError: Failed to fetch

and the library isn't recovering from that error (automaticSilentRenew is true) and token renewal is working fine otherwise. I guess the timeout solution implemented in Waking up from sleep #251 doesn't help because the connection can't be established in the first place.

Thanks, Daniel

CanD42 avatar Jan 16 '24 09:01 CanD42

Running into the same issue on version 2.4.0 and node v16.20.1.

My current workaround is to attempt a token renewal on token expiry event by explicitly waiting for both network connectivity and internet access. Looks something like this:

this.userManager.events.addAccessTokenExpired(() => {
  new Promise<void>(resolve => {
    const interval = setInterval(() => {
      // navigator.onLine only checks for network connectivity but not internet access. Need to manually check
      if (navigator.onLine)
        fetch(new Request(window.location.origin)) // whatever endpoint allowed by cors policy you want to hit
          .then(v => {
            if (v.ok) {
              clearInterval(interval)
              resolve()
            }
          })
    }, 1000)
  }).then(() => {
    console.debug('Token expired. Attempting silent renew.')
    this.userManager.signinSilent().catch((e) => {
      console.error('Silent renew error from access token expiry event: ', e)
      // if you end up here you might want to start a signout process
    })
  })
})

ykageyama-mondo avatar Jan 17 '24 00:01 ykageyama-mondo

This looks promising. The offline status is correctly telling that the browser is online. However, the Mozilla docs states "you cannot assume that a true value necessarily means that the browser can access the internet." https://developer.mozilla.org/en-US/docs/Web/API/Navigator/onLine

So what about "pinging" the well-known endpoint additionally when navigator.onLine is true

    fetch('<op-well-known-endpoint>', {
        method: 'GET',
        mode: 'no-cors',
    }).then((result) => {
        console.log("online")
    }).catch(e => {
        console.log("offline")
    })

It's an additional call but makes sure that there is really an internet connection. Would that work?

CanD42 avatar Jan 17 '24 18:01 CanD42

Hi, is there any update here? we want to go live next month with our solution.

CanD42 avatar Feb 28 '24 16:02 CanD42

@CanD42 ~In https://github.com/authts/oidc-client-ts/issues/1435 I suggested to emit an USER_UNLOADED event when the refresh token requests fails. Would this behavior be ok for your use case too?~

Sorry, I noticed there is already a special silentRenewError event, which can be used in my use case.

PSanetra avatar Mar 12 '24 11:03 PSanetra