Guide: Migrate your custom Rules to Auth0 Actions

Due to the future deprecation of Auth0 Rules, users are required to migrate their custom Rules to the latest Auth0 Actions.

This post will guide you through this migration process.

Auth0 Rule to migrate

Name: Add user information to access token

Script:

function (user, context, callback) {
  // This rule adds the authenticated user's email address to the access token.

  var namespace = 'https://mydomain.com/';

  context.accessToken[namespace + 'email'] = user.email;
  context.accessToken[namespace + 'email_verified'] = user.email_verified;

  return callback(null, user, context);
}

Create Auth0 custom Action

  1. Go to Actions → Flows → Login

  2. Go to the “Custom” tab and click “Create Action”

  3. Give the action a name

  4. Here’s how the Action version of the old Auth0 Rule looks 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://mydomain.com';
    
        api.accessToken.setCustomClaim(`${namespace}/email`, event.user.email);
        api.accessToken.setCustomClaim(`${namespace}/email_verified`, event.user.email_verified);
      }
    };
    
  5. You can click on the “Test” button before deploying

  6. After you’ve the deployed the Action, you want to drag it into the Login flow

  7. Make sure to click “Apply” in order to save the latest changes

  8. Now you can disable the old Rule, since the new Action that we created has already been deployed

Additional resources

You can take a look at the docs for more comprehensive code examples on the new Auth0 Actions:

3 Likes

Thanks for sharing it with the rest of community Nikolay!

2 Likes

I have a rule straight from the documentation Change User Pictures

function (user, context, callback) {
  if (user.user_metadata.picture)
    user.picture = user.user_metadata.picture;

  callback(null, user, context);
}

How do I migrate that?
I suppose it is adding attributes to the identity token instead of access token

Thank you for sharing Nikolay! This is a great guide. Just one clarification, we have not formally announced the feature deprecation of Rules & Hooks yet. However, Actions is very much our flagship extensibility product and I highly encourage everyone who has used Rules & Hooks in the past to migrate to Actions where they can take advantage of a greatly improved developer experience.

2 Likes

Thanks for following up on that Darin!

We now have an official guide to migrating from Rules to Actions!

2 Likes