Skip to content
Advertisement

Not able to return authResponse from auth0-js

I’m trying to implement an login mechanize and not able to return a value from the callback function. I’m using this npm package: auth0-js. There’s two files in my setup. The first one is authService.js where I have my login logic:

JavaScript

The second one: index.js

JavaScript

I tried returning the value from the callback, as well as assigning the result to a local variable inside the function and returning that one, but none of those ways actually return the response. I saw this answer, but it didn’t help much.

Advertisement

Answer

In the following snippet, both lines will always try to run at the same time.

JavaScript

console.log(authResponse) is not going to wait for handleLogin to finish and return authResult

authResult is only available inside of the callback

JavaScript

If you want your code synchronously, or have handleLogin(auth0Client, user); resolve before having the rest of the code run, you can turn handleLogin into a function that returns a Promise that resolves with the authResponse. This will cause console.log(authResponse) to wait for handleLogin(auth0Client, user);.

JavaScript
JavaScript

If you are doing this in Node, you have to make sure that you call this in an async function. Placing it inside a wrapper function should suffice

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement