Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In auth0 lock, how to refresh the id_token?

Tags:

auth0

I am building a cordova mobile app and trying to use the auth0 lock API. I am having trouble with the refresh token. I can retreive the refresh token in the authResult but cannot figure out how to actually refresh the id_token ( I suppose i could write the REST calsl myself )

In the v9 docs, it seems there used to be a method: https://auth0.com/docs/libraries/lock/v9/using-a-refresh-token

lock.getClient().refreshToken(refresh_token, function (err, delegationResult) {
  // Get here the new JWT via delegationResult.id_token
});

However in lock v10 it seems this method doesn't exist any more: https://auth0.com/docs/libraries/lock/v10/api

Can anyone advise if there is a way to refresh the token using the lock API?

like image 530
C. Woza Avatar asked Sep 25 '16 23:09

C. Woza


1 Answers

First, you need to either have included Auth0's script tag in your HTML:

 <script src="https://cdn.auth0.com/js/lock/10.8/lock.min.js"></script>

Or, if you've installed from npm, you can require Auth0:

 var Auth0 = require("auth0-js");

In V10, you create an instance of the Auth0 client (separate from the Auth0Lock instance) which has a function refreshToken():

var auth0 = new Auth0({clientID: YOUR_CLIENT_ID, domain: YOUR_AUTH0_DOMAIN});
...
auth0.refreshToken(refresh_token_here, (err, resp) => {
    // resp: {expires_in: 36000, id_token: "id_token here", token_type: "Bearer"}
}

The same can also be achieved by using the getDelegationToken() function:

auth0.getDelegationToken({
    client_id: YOUR_CLIENT_ID,
    grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
    refresh_token: refresh_token_here,
    scope: "openid",
    api_type: "auth0"
  }, (err, resp) => {
    // resp: {expires_in: 36000, id_token: "id_token here", token_type: "Bearer"}
  });
like image 106
Andrew Avatar answered Jan 02 '23 12:01

Andrew