Rule to add user email to access token?

Hi, I found a few posts on this topic already, but I can’t seem to get this to work. I have a custom rule to add user.email to the access token instead of fetching this information from userinfo. The rule looks like this:

function (user, context, callback) {
    context.accessToken["https://MY_DOMAIN_DOT_COM/claims/email"] = user.email;
    return callback(null, user, context);
}

Yet still, I see nothing in the access token. Any ideas?

Hi,

This works in my tenant, I had to change the custom claim to single quotes.

function (user, context, callback) {
	context.accessToken['https://MY_DOMAIN_DOT_COM/claims/email'] = user.email;
return callback(null, user, context);
}

Including a link to our rules debugging doc in case it is helpful.

2 Likes

Thanks for sharing that solution Marcus!

Thanks for update and quick reply, i found lots of information here, Really appreciate for help.

1 Like

Perfect! Glad to hear that!

Deleting previous message due to SPAM reasons.

For the people who want to migrate their Auth0 Rules, due to the deprecation on Feb 25, 2022, here’s how the Auth0 Action version would look like:

exports.onExecutePostLogin = async (event, api) => {
  // This rule adds the authenticated user's email address to the access token.
  if (event.authorization) {
    const namespace = 'https://MY_DOMAIN_DOT_COM';
    api.accessToken.setCustomClaim(`${namespace}/claims/email`, event.user.email);
  }
};

Detailed migration guide can be found here:

1 Like

Thanks for sharing it with the rest of community!

1 Like

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