Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 309
Hiren Makwana Avatar asked Jul 10 '19 12:07

Hiren Makwana


2 Answers

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.

like image 175
stackOp Avatar answered Oct 23 '22 19:10

stackOp


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));   }, }); 
like image 28
chetan mahajan Avatar answered Oct 23 '22 20:10

chetan mahajan