Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling refreshToken? #125

Open
ajmas opened this issue Mar 4, 2022 · 4 comments
Open

Handling refreshToken? #125

ajmas opened this issue Mar 4, 2022 · 4 comments

Comments

@ajmas
Copy link

ajmas commented Mar 4, 2022

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
Copy link
Contributor

baltom commented Mar 4, 2022

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
Copy link
Author

ajmas commented Mar 4, 2022

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
Copy link

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

@ylighgh
Copy link

ylighgh commented Dec 26, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants