Auth0 Home Blog Docs

Is it possible to Renew tokens on page load?


#1

Hi!

I’m using the latest angular-auth0 & angular-jwt. I’m following the guides to setup my authentication flow and so far so good. I’m using passwordless email code authentication without Lock, so its included on my own page with custom design. Works with angularAuth0.passwordlessStart & passwordlessLogin.

By default the user is kept logged in for 2 hours, but i want to keep them signed in, especially because passwordless login is a bit slower compared to just entering the password. So i found the guide about renewing the tokens here: https://auth0.com/docs/quickstart/spa/angularjs/05-token-renewal

My question is, based on the scheduleRenewal function, this will only run if i’m on the page, without closing it. If the token expired and i reload the page, i won’t get new tokens, since the renewToken function newer runs. Can i simply add the renewToken function in the else statement, so it tries to renew on page load if the token is expired? Like this:

  function scheduleRenewal() {
    var expiresAt = JSON.parse(localStorage.getItem('expires_at'));
    var delay = expiresAt - Date.now();
    if (delay > 0) {
      tokenRenewalTimeout = setTimeout(function() {
        renewToken();
      }, delay);
    } else {
      renewToken();
    }
  }

I looked through other examples of renewing the tokens, but all examples uses the code above, which doesn’t run on page load. Wondering whats the right solution in my case.

Thanks!


#2

I tested the above method and it works, just wondering if might cause any issues in the future which is why it was left out from the examples.