Adding custom claims to tokens

Problem statement: How do I add custom claims to an Access and/or ID Token with Actions?

Solution: Custom claims can be added to an Access/ID Token in a namespaced format by utilizing a Post Login Action. For example:


exports.onExecutePostLogin = async (event, api) => {

const namespace = 'https://myapp.example.com';
const { favorite_color, preferred_contact } = event.user.user_metadata;

if (event.authorization) {

    // Set claims in ID token

    api.idToken.setCustomClaim(`${namespace}/favorite_color`, favorite_color);

    api.idToken.setCustomClaim(`${namespace}/preferred_contact`, preferred_contact);

    // Set claims in access token

    api.accessToken.setCustomClaim(`${namespace}/favorite_color`, favorite_color);

    api.accessToken.setCustomClaim(`${namespace}/preferred_contact`, preferred_contact);


  }

};

Reference Materials :books: :

Related FAQs:

1 Like