AWS Cognito: Add custom claim/attribute to JWT access token

14,988

Solution 1

Custom attributes are not available in Cognito access token. Currently it is not possible to inject additional claims in Access Token using Pre Token Generation Lambda Trigger as well. PreToken Generation Lambda Trigger allows you to customize identity token(Id Token) claims only.

Solution 2

You can use ID token to get the token with custom attributes.

Access tokens are not intended to carry information about the user. They simply allow access to certain defined server resources.

You can pass an ID Token around different components of your client, and these components can use the ID Token to confirm that the user is authenticated and also to retrieve information about them.

How to retrieve Id token using amazon cognito identity js

cognitoUser.authenticateUser(authenticationDetails,{
  onSuccess: function(result) {
    var accessToken = result.getIdToken().getJwtToken();
    console.log('accessToken is: ' + accessToken);
  },
  onFailure: function(err) {
    alert(err.message || JSON.stringify(err));
  },
});
Share:
14,988
Hiren Makwana
Author by

Hiren Makwana

Software Engineer with 6+ years of experience & worked in Open source technologies. Professional career started with Laravel and Symfony, later on moved in NodeJS, MongoDB, PostgreSQL, etc. Also having hands on experience in cutting-edge technologies like AWS serverless, kinvey mBaaS, Microservices, Docker, etc. I can easily learn and adopt other technologies. To work for an organization which provides me the opportunity to improve my skills and knowledge to grow along with the organization objectives.

Updated on July 02, 2022

Comments

  • Hiren Makwana
    Hiren Makwana almost 2 years

    My app creates a custom attribute "userType" for each new signed-up user. Now I would like this "userType" claim/attribute to be added to the JWT access token whenever the user signs in or the token gets refreshed.

    Is there an option to tell cognito to add my custom claim/attribute to the JWT access token? (Without a pre token generation Lambda)