dsb-norge / vue-keycloak-js

A Keycloak plugin for Vue 2
https://www.dsb.no
ISC License
272 stars 99 forks source link

Handling refreshToken? #125

Open ajmas opened 2 years ago

ajmas commented 2 years ago

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);
baltom commented 2 years ago

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.

ajmas commented 2 years ago

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.

oleaasbo commented 2 years ago

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

ylighgh commented 10 months ago

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)
  },