Migrating Auth0 Hooks to Auth0 Actions
Tags: security, programming, webdev
Published: 2022-04-26 19:37:49

### Learn some guidelines to migrate your Auth0 Hooks to Auth0 Actions successfully.

### <br>

Auth0 Hooks completes the customization capability of Rules in the Auth0 platform. However, Auth0 Actions provide a unified and powerful environment to perform the same tasks you can do with both Rules and Hooks. Learn how to migrate your existing Auth0 Hooks into Auth0 Actions with this article.

## Auth0 Actions and Customization

One of the most compelling Auth0 features is its support for customization. Beyond the basic login and logout functionality for your applications, **you can tweak the standard flow of some of Auth0's behind-the-scenes processes to meet your specific needs**. For example, you can do extra authentication checks or perform specific operations after a user registers with your application.

The features that many Auth0 customers are familiar with to make those customizations are [Auth0 Rules](https://auth0.com/docs/customize/rules) and [Auth0 Hooks](https://auth0.com/docs/customize/hooks). Auth0 Rules allow you to customize the authentication flow, while Auth0 Hooks enable you to customize the Auth0 behavior for some [extensibility points](https://auth0.com/docs/customize/hooks/extensibility-points), such as pre and post user registration, password change, phone message sending, and so on.

Now you have [Auth0 Actions](https://auth0.com/docs/customize/actions), an Auth0 feature that allows you to achieve the same customization goals as Rules and Hooks. **Auth0 Actions provide you with a unified development environment** to customize the extensibility points you were used to doing with Rules and Hooks. Actions not only offer you the same power as Rules and Hooks. They also give you some additional features that simplify your development experience:

- Actions provide you with an **integrated version system**. You can modify an Action without affecting the one in production, create multiple versions, and restore a previous one.
- The Actions editor supports code hints, all npm packages, and isolated secrets management. 
- You can take advantage of a growing number of [no-code Actions integrations](https://marketplace.auth0.com/features/actions) from the [Auth0 Marketplace](https://marketplace.auth0.com/), which solve common identity use cases such as consent management, progressive profiling, and user verification. Built by Partners, those integrations enable developers to go-to-market faster while reducing development and maintenance costs associated with custom code.

Read [this blog post for a quick introduction to Auth0 Actions](https://auth0.com/blog/introducing-auth0-actions/).

> While Auth0 Actions bring you an improved customization environment, Rules and Hooks are still available, and they all can live together. <u>Remember that Rules and Hooks execute before Actions</u>.

Auth0 Actions are a recommended alternative to Auth0 Rules and Hooks. If you customized Auth0 flows with Rules and Hooks and now want to migrate to Auth0 Actions, you are in the right place.

This article provides some guidelines for migrating your Auth0 Hooks to Auth0 Actions. Check out [this article to migrate your Auth0 Rules to Auth0 Actions](https://auth0.com/blog/migrating-auth0-rules-to-auth0-actions/).

## Code Conversion Guidelines

To convert your Auth0 Hooks into an equivalent Auth0 Action, follow the guidelines in this section.

> This article assumes that you have a basic understanding of how to create a Hook and how to create an Action. If not, please refer to Auth0 documentation about [Hooks creation](https://auth0.com/docs/customize/hooks/create-hooks) and [Actions writing](https://auth0.com/docs/customize/actions/write-your-first-action). In both cases, you need an Auth0 account. If you don't have it, <a href="https://auth0.com/signup" data-amp-replace="CLIENT_ID" data-amp-addparams="anonId=CLIENT_ID(cid-scope-cookie-fallback-name)">you can get one for free</a>.

### Mapping Hook types to Action Triggers

The first thing you need to do is identify the Action Trigger that corresponds to your [Hook type](https://auth0.com/docs/customize/hooks/extensibility-points). [Auth0 Action Triggers](https://auth0.com/docs/customize/actions/flows-and-triggers) are events that happen within Auth0 flows. The following table maps each Hook type to the respective Action Trigger:


<table>
  <tr>
    <th>Hook type</th>
    <th>Action Trigger</th>
  </tr>
  <tr>
    <td>
      Pre-User Registration
    </td>
    <td>
      Pre User Registration
    </td>
  </tr>
  <tr>
    <td>
      Post-User Registration
    </td>
    <td>
      Post User Registration
    </td>
  </tr>
  <tr>
    <td>
      Post-Change Password
    </td>
    <td>
      Post Change Password
    </td>
  </tr>
  <tr>
    <td>
      Send Phone Message
    </td>
    <td>
      Send Phone Message
    </td>
  </tr>
  <tr>
    <td>
      Client Credential Exchange
    </td>
    <td>
      M2M/Client-Credentials
    </td>
  </tr>
</table>
<br/>

The names for Hook types and Action Triggers are almost identical. So, when you are going to convert your Hook into an Action, you need to [create the Action](https://auth0.com/docs/customize/actions/write-your-first-action) by selecting the right Trigger from the list, as shown in the following picture:

![Auth0 Action Trigger list](https://images.ctfassets.net/23aumh6u8s0i/5gxQgwFP7FzKLPr1UVFgI8/79ff2cae324b70c21740bac057b8e365/select-auth0-action-trigger.png)

When you select a Trigger, you get an empty [named export](https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export) in the Action editor. The name of the specific export depends on the selected Trigger. For example, if you convert your Post-User Registration Hook into a Post User Registration Action Trigger, you will get the following code in your Action editor:

```javascript
exports.onExecutePostUserRegistration = async (event) => {
  
};
```

You can see this mapping between your Hook type and the corresponding export name in the following table:

<table>
  <tr>
    <th>Hook type</th>
    <th>Action Trigger</th>
    <th>Action named export</th>
  </tr>
  <tr>
    <td>
      Pre-User Registration
    </td>
    <td>
      Pre User Registration
    </td>
    <td>
      `onExecutePreUserRegistration`
    </td>
  </tr>
  <tr>
    <td>
      Post-User Registration
    </td>
    <td>
      Post User Registration
    </td>
    <td>
      `onExecutePostUserRegistration`
    </td>
  </tr>
  <tr>
    <td>
      Post-Change Password
    </td>
    <td>
      Post Change Password
    </td>
    <td>
      `onExecutePostChangePassword`
    </td>
  </tr>
  <tr>
    <td>
      Send Phone Message
    </td>
    <td>
      Send Phone Message
    </td>
    <td>
      `onExecuteSendPhoneMessage`
    </td>
  </tr>
  <tr>
    <td>
      Client Credential Exchange
    </td>
    <td>
      M2M/Client-Credentials
    </td>
    <td>
      `onExecuteCredentialsExchange`
    </td>
  </tr>
</table>
<br/>

The body of your Hook will go into the body of your newly created Action, with some changes as you will learn as you go along.

[Read more...](https://auth0.com/blog/migrating-auth0-hooks-to-auth0-actions/?utm_source=content_synd&utm_medium=sc&utm_campaign=actions)