Custom Actions - What happens with errors?

I created a custom action so when a new user registers I create a user doc in my apps database.

During my test of the action it worked and the test data was saved to my database… but what happens if there is an error in my action?

I don’t know how to handle the error?

The ultimate goal of my action to to create the user doc and attach the userId in my database onto the Auth0 user metadata so when I make calls to my api… it’s super easy to query my database.

I am guessing if creating the user document in the action errors out for some reason… I can attach an empty string or null to the Auth0 user metadata.

When I get the user in my app, I can check for null and if null… prompt them that there was an error and they need to finish creating their account… and handle this with my own api and the user management api in Auth0 to add the userId to the user metadata.

Is this the “correct” why to handle an error?

Or do errors kill the action?

Hi @danboyle8637,

I think your solution sounds like it would work well.

One thing to consider is that the post-user registration trigger is non-blocking, meaning errors won’t interrupt the login flow. Since it sounds like there is functionality in your app that requires this Action to be successful, another option would be to use a post-login action instead which runs synchronously and will interrupt the login flow.

You can check if it the first time a user has logged in with event.stats.logins_count (e.g. if (event.stats.logins_count === 1) ) {...).

If an error occurs in the post-login action, you can throw an error and the user will be returned to your app with the error message in a URL query string param. Your app can check for this error message to display the appropriate message and properly create the account.

exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count !== 1) {
    return;
  }

  try {
    // ... make request, etc.
  } catch (e) {
    console.log(e);
    api.access.deny('Could not create user doc');
  }
};
2 Likes

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