How to add email to access token using auth0 action Post-login event

I want to add the user’s email to my API access token. earlier using rules we used to do this but now since rules deprecated we have to add actions so i have created a Post-Login Action to do so:
below is the code for it i have added

exports.onExecutePostLogin = async (event, api) => {
  // This rule adds the authenticated user's email address to the access token.


    // This rule adds the authenticated user's email address to the access token.
  if (event.authorization) {
  const namespace = 'https://thepickleballwizard.com';
    api.accessToken[`${namespace}/email`] =  event.user.email;
  }
};

above code not adding any email to access token what i am missing

You need to use setCustomClaim(). Here’s how to do it for both your ID and access tokens:

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://thepickleballwizard.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/email`, event.user.email);
    api.accessToken.setCustomClaim(`${namespace}/email`, event.user.email);
  }
}
1 Like

yes its worked thanks for the help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.