How to import custom `PreUserRegistrationError` class

How does one import the custom PreUserRegistrationError class for use in the pre-user registration hook? ESlint in the hook IDE throws a module undefined error.

I’m doing something similar to what’s shown in the sample script here:

Hey @happy.sisyphus, Welcome to the Auth0 Community!

To use the PreUserRegistrationError class , you don’t need any additional import, the sample provided in the link shared by you should suffice. Based on the logic you are planning to implement you can directly use the class to throw the custom error.

This custom error functionality only works with New Login experience.

Regards,
Sidharth

1 Like

Hmm. I’m still getting the ESLint error PreUserRegistrationError is not defined [no-undef], even though I changed to the New Login experience.

1 Like

Hey @happy.sisyphus, can you DM me your hook code to check?

1 Like

@sidharth.chaudhary When I tried to create the Pre Auth Hook, I was getting the ESLint error.

Below is a sample code:

module.exports = function (user, context, cb) {
  var response = {};

  response.user = user;

  // Add user or app metadata to the newly created user
  // response.user.user_metadata = { foo: 'bar' };
  // response.user.app_metadata = { vip: true, score: 7 };

  // Deny the user's registration and send a localized message to New Universal Login
  var denyRegistration = true;
  if (denyRegistration) {
     const LOCALIZED_MESSAGES = {
       en: 'You are not allowed to register.',
       es: 'No tienes permitido registrarte.'
     };
  
     const localizedMessage = LOCALIZED_MESSAGES[context.renderLanguage] || LOCALIZED_MESSAGES['en'];
     return cb(new PreUserRegistrationError('Denied user registration in Pre User Registration Hook', localizedMessage));
  }

  cb(null, response);
};

@chirag, Welcome to the Auth0 Community!

Hook runs correctly Irrespective of the ESLInt Error ,Not sure as to why the ESlint error is coming, I was also able to see it, might need further investigation. But the Hook is running correctly at runtime and PreUserRegistrationError class is also throwing the error correctly on the New Universal login page as well as showing the error on the tenant logs

Hi, I also need to import the custom PreUserRegistrationError class, and I’m afraid I can’t really accept the previous answer as I need to be able to run unit tests against this code. The inability to get this class means I cannot test my hook code as-is (which is 100% a requirement for my production codebase).

1 Like

I have the same issue. I’m writing local unit tests and need to get this from somewhere? Any solution or workaround?

2 Likes

We also have this observation and the hook linting within auth0 webtask IDE is complaining that this class is never defined. But ignoring the lint issues, this error class works perfectly at runtime.

With regards to the unit test, we’re able to manage to export the class into the global variable within our test script. This allows us to test the negative scenario of our hook.

e.g.

test.js

class PreUserRegistrationError extends Error {
  constructor(message, description) {
    super(message);
    this.name = "PreUserRegistrationError";
    this.description = description;
  }
}

global.PreUserRegistrationError = PreUserRegistrationError;

...

// your tests should come after the global import