vue-keycloak-js icon indicating copy to clipboard operation
vue-keycloak-js copied to clipboard

Handling refreshToken?

Open ajmas opened this issue 2 years ago • 4 comments

As part of vue-keycloak-js, what is the suggested way to handle the refresh token?

Right now I am using the following code, but I am not sure if it is the right approach:

    await (new Promise<any>((resolve, reject) => {
      app.use(VueKeyCloak, {
        init: initOptions,
        config: keycloakConfig,
        logout: {
          redirectUri: window.location.origin
        },
        onReady (keycloak: KeycloakInstance) {
          // Make this available to anyone importing the boot file
          keycloakInstance = keycloak;
          resolve(keycloak as any);
        },
        onInitError (error: any) {
          reject(error);
        }
      });
    }));

    setInterval(() => {
      const keycloak = keycloakInstance;
      console.log('interval:keycloak', keycloak);
      if (keycloak) {
        keycloak.updateToken(70).then((refreshed: boolean) => {
          console.log('interval:refreshed', refreshed);
          if (refreshed) {
            logger.info(`Token refreshed ${refreshed as any as string}`);
          } else {
            const expiry = keycloak.tokenParsed?.exp as number || 0;
            const timeSkew = keycloak.timeSkew as number || 0;
            const seconds = Math.round(expiry + timeSkew - new Date().getTime() / 1000);
            logger.warn(`Token not refreshed, valid for ${seconds} seconds`);
          }
        }).catch(() => {
          logger.error('Failed to refresh token');
        });
      }
    }, 30000);

ajmas avatar Mar 04 '22 03:03 ajmas

You should not really have to worry about the refresh token? as the plugin is already handling it. Updating the access token accordingly. Meaning you just use that token correctly and it should automatically update whenever its expiry is closing in (< 60 seconds), and a refresh token is used.

baltom avatar Mar 04 '22 06:03 baltom

Ok, I’ll remove the refresh handler, but still trying to understand why I am finding myself with an expired token from time to time.

ajmas avatar Mar 04 '22 13:03 ajmas

I'm also getting expired token from time to time. Does not refresh until i logout and on again

oleaasbo avatar Sep 27 '22 07:09 oleaasbo

You should not really have to worry about the refresh token? as the plugin is already handling it. Updating the access token accordingly. Meaning you just use that token correctly and it should automatically update whenever its expiry is closing in (< 60 seconds), and a refresh token is used.

How do I update my stored Token when the plugin automatically refreshes the token?

  onReady: (keycloak) => {
    var refreshTime = Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) * 1000
    if (keycloak.token) {
      console.log(getCurrentDateTime())
      console.log(keycloak)
      storage.set(Bearer, keycloak.token)
    }
    storage.set(USER_NAME, Vue.prototype.$keycloak.userName)
    setInterval(() => {
      keycloak.updateToken(30).then((refreshed) => {
        if (refreshed) {
          console.log(getCurrentDateTime())
          console.log('Token refreshed: ' + refreshed)
          console.log('and here is the new token: ', keycloak.token)
          refreshTime = Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) * 1000
          storage.set(Bearer, keycloak.token)
          storage.get(Bearer)
        } else {
          console.log('Token not refreshed, valid for ' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds')
        }
      }).catch(() => {
          console.log('Failed to refresh token')
      })
    }, refreshTime)
  },

ylighgh avatar Dec 26 '23 06:12 ylighgh