How to return error from auth0 callback

Is there a way to return the error from the callback? Please see notes in the following signup code

    // signup method takes email password and db connection
      const signUp = (email, password) => {
        console.log("the user called signup method")
        auth0Client.redirect.signupAndLogin({
          "connection": 'Username-Password-Authentication',
          "email": email,
          "password": password
        }, function (err) {
          if (err) {
// IS THERE A WAY TO RETURN THE ERROR CODE
// need return something, can just simply be "Error"
// using this to basically reset by submit button from disabled on all other api calls
// all others I am using async await, but since auth0 doesn't support not sure
// what I would be doing here 
            return err.code
          }
          else {
            setUser(getUser());
            setAuthenticated(true);
            console.log("finished sign up")
          }
        })
      };

Hi @stephen1,

Thanks for reaching out. I think I could use some more context or an example in regards to this issue. It looks like you have the app setup to return err.code, is that not happening?

Thanks,
Dan

So it took me a couple days of deep debugging for this problem, but basically I have to wrap the auth0 function in a Promise since it just returns a callback and I need to wait for the response. Sucks there is no async await support for the library and I think my code may be a bit spaghetti, but this works for me:

const signUp = async (email, password) => {
    console.log("the user called signup method")
    await new Promise(function (resolve, reject) {
      auth0Client.redirect.signupAndLogin({
        "connection": 'Username-Password-Authentication',
        "email": email,
        "password": password
      }, (err) => {
        if (err) {
          return reject(new Error(err.code))
        }
        else {
          setUser(getUser());
          setAuthenticated(true);
          console.log("finished sign up")
        }
      })
    });
  }

I see. Glad you were able to figure it out.

We recently launched a new SPA sdk called auth0-spa-js. You should take a look. It has some limitations that auth0.js does not, but I think it should support your request here with async await.

Hope this helps!

Thanks,
Dan

So the main limitation with auth0-spa-js and why I couldn’t use it off the bat is around having a custom flow. It works fine for the lock, but no documentation is in place at all about how to have a custom login form at least as of June 2019. Maybe that has changed?

I see. Auth0.js will be the best option for you at the current time.

It sounds like there is a feature gap here, and I would recommend submitting a ticket via our feedback page. This is a direct line to our product team and helps us determine customer needs and gauge demand.

Thanks,
Dan

@dan.woda done, thanks!

1 Like

We appreciate it. Thanks @stephen1!