Alternative to using rule to attach email to access tokens

I’m currently using the rule to attach an access token but I’ve seen that rules are going to be deprecated, what would the alternative using an action be for this?

Hello @jmdot welcome to the community!

Similar to a rule, you will just need to add email as a custom claim using an Action - A Post Login Action should work for this and look like the following:

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://myapp.example.com/';
  const email = event.user.email;

  if (email) {
    api.accessToken.setCustomClaim(`${namespace}email`, email);
  }
};

Since email is not a restricted claim you can also forego using a namespace:

exports.onExecutePostLogin = async (event, api) => {
  const email = event.user.email;

  if (email) {
    api.accessToken.setCustomClaim('email', email);
  }
};

1 Like

Awesome, thank you very much I’m going to give this a shot.

I’m facing another similar challenge, I currently have a rule which runs after users signup in order to create user accounts in our database. We want to replace this with an action but when we tried we noticed that actions aren’t being triggered when a user does a social sign up. What is the alternative route that we should take as we’re aware that rules will be being replaced next year which will break this functionality that we require?

Hey @jmdot no problem, happy to help!

Neither Pre nor Post Registration Actions will run for Social connections - You will need to look into using a Post Login Action instead.

1 Like

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