Amplify-js: Refresh id token after updating user attributes

Created on 14 Nov 2019  路  3Comments  路  Source: aws-amplify/amplify-js

* Which Category is your question related to? *
Auth
* What AWS Services are you utilizing? *
Cognito
* Provide additional details e.g. code snippets *
How do I get an updated idToken after updating user attributes:

let user = await Auth.currentAuthenticatedUser();
const result = await Auth.updateUserAttributes(user, {
    'custom:merchant_id': merchant_id,
});
console.log(result); // SUCCESS
user = await Auth.currentAuthenticatedUser();
console.log(user);

This works as expected but it will not update ID (identification) token used by Amplify...

Now that old ID token is sent the claims in there are not updated, server receives the old claims.

Is there any way to refresh / regenerate the id token?

Auth question

Most helpful comment

This will give the updated attributes of the current authenticated user but not refresh the id/jwt token. I ended up doing this:

import {
  Auth
} from 'aws-amplify';

class AuthHelper {
  static async refreshSessionPromise(refreshToken) {
    return new Promise(async (resolve, reject) => {
      const user = await Auth.currentAuthenticatedUser();
      return user.refreshSession(refreshToken, async (err, data) => {
        if (err) {
          reject(err);
        } else {
          resolve(data); // THIS IS YOUR REFRESHED ATTRIBUTES/GROUPS
        }
      });
    });
  }
  static async refreshCurrentSession() {
    const session = await Auth.currentSession();
    return this.refreshSessionPromise(session.getRefreshToken());
  }
}

export default AuthHelper;

All 3 comments

I think the data in server got updated but data in cache not updated

call this once to update the current cache

await Auth.currentAuthenticatedUser({ bypassCache: false });

This will give the updated attributes of the current authenticated user but not refresh the id/jwt token. I ended up doing this:

import {
  Auth
} from 'aws-amplify';

class AuthHelper {
  static async refreshSessionPromise(refreshToken) {
    return new Promise(async (resolve, reject) => {
      const user = await Auth.currentAuthenticatedUser();
      return user.refreshSession(refreshToken, async (err, data) => {
        if (err) {
          reject(err);
        } else {
          resolve(data); // THIS IS YOUR REFRESHED ATTRIBUTES/GROUPS
        }
      });
    });
  }
  static async refreshCurrentSession() {
    const session = await Auth.currentSession();
    return this.refreshSessionPromise(session.getRefreshToken());
  }
}

export default AuthHelper;

Should be fixed now in the latest unstable release. Will be released to stable in the near future as well. Closing this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rygo6 picture rygo6  路  3Comments

callmekatootie picture callmekatootie  路  3Comments

guanzo picture guanzo  路  3Comments

benevolentprof picture benevolentprof  路  3Comments

oste picture oste  路  3Comments