Auth0 Home Blog Docs

How to return error from Auth0 hooks

auth0-lock
hooks
error

#1

If I want to return custom error from Rules I simply do callback(new UnauthorizedError('Custom error message here')) but how do I do the same thing with Hooks?

callback('error message');
callback(new Error('error message'));

Those didn’t worked and UnauthorizedError is undefined in Hooks. Whatever I do, on front-end side I always get WE'RE SORRY, SOMETHING WENT WRONG WHEN ATTEMPTING TO SIGN UP. and when I inspect result of requested I see that there is no difference, each time InternalExtensibilityError comes.

Why do I want to return error from Hooks? I run extra validation for sign-up there.


#2

In Hooks, function must call the callback with two arguments: an error, and the result. In order to return an error, you can simply the callback as follows:

cb("Unauthorized Signup");

#3

https://manage.auth0.com/#/hooks

Pre User Registration
This hook allows custom code to prevent creation of a user in the database or to add custom app_metadata or user_metadata to a newly created user.

One (Post User Registration) out of three Hooks is executed asynchronously.

As I said, the reason why I want to do it in Hook is sign-up validation and in Rules it’s not possible to do that at this time. You can only prevent user from login in in rules.


#4

Please, let’s stop this @prashant before I start to question if I am insane. I came here to get help in implementing the tool that I indent to pay for and instead I’m wasting my time on explaining you why your answers are wrong.

First answer that you gave me (and edited so it can’t be seen anymore) was the worse I ever got from support because just going to “hooks” page could prove you wrong.

If you would take a look at my question, it’s very short. I wrote down what I have tried and you might be surprised here but if you rename third argument in exported function from “callback” to “cb” it’s going to work exactly the same.

But I did it. I renamed it. That console.log is just so I can check in Real-time Webtask Logs that this code was run.

module.exports = function (user, context, cb) {
  console.log(user);
  cb("Unauthorized Signup");
};

but I’m still getting as a response for sign-up call

{"name":"InternalExtensibilityError","statusCode":500,"code":"server_error","message":"Internal server error","fromSandbox":true}

Could you please @prashant ask someone technical in your team who knows the product or simply tell me that it’s impossible.


#5

I am having the same frustration, what is the point of having rules if I can’t prevent a user from signing up. It almost defeats the purpose of using auth0.


#6

same issues here. Could it have anything to do with version of lock I’m using?


#7

Experiencing the same issue, have there been any updates for this?


#8

I’m waiting for an answer to this as well.


#9

The text entered in the error argument of the callback is not propagated back to the client that made the request so there’s no way of showing a relevant message to the user letting them now why they were not allowed to sign up, which makes it rather useless at the moment.


#10

I have added a registration code to my signup form (additionalSignUpFields). If validation fails then cb(“Invalid registration code.”) is called from the hook. Problem is the message does not show up in the lock10 hosted. Instead the message “WE’RE SORRY, SOMETHING WENT WRONG WHEN ATTEMPTING TO SIGN UP” is displayed.
How do you pass an error message from a hook?


#11

I have added a registration code to my signup form (additionalSignUpFields). If validation fails then cb("Invalid registration code.") is called from the hook. Problem is the message does not show up in the lock10 hosted. Instead the message WE'RE SORRY, SOMETHING WENT WRONG WHEN ATTEMPTING TO SIGN UP is displayed. How do you pass an error message from a hook?


#12

We have been waiting on an answer to this for months.


#13

yes i see that. well they are covered because it is a beta environment. i did see somewhere there is no way to propagate a message from hooks to the hosted pages. it is a gap they know about and have discussed internally.


#14