This is more of a conceptual question. I’m very intrigued by this piece of code from the reference docs
document.getElementById('login').addEventListener('click', () => {
auth0.loginWithRedirect({
redirect_uri: 'http://localhost:3000/'
}).then(token => {
//logged in. you can get the user profile like this:
auth0.getUser().then(user => {
console.log(user);
});
});
});
However as per the library docs (and the method definition as well) loginWithRedirect
returns Promise<void>
. Is that code wrong?
Background:
I have followed the react quickstart and set up my app to work with loginWithPopup
. All good so far.
However I wanted to test loginWithRedirect
as well. My first step was to just replace one for another like this:
const loginWithPopup = async (params = {}) => {
setPopupOpen(true);
try {
// await auth0Client.loginWithPopup(params); // <---- original
await auth0Client.loginWithRedirect(params); // <---- new
} catch (error) {
console.error(error);
} finally {
setPopupOpen(false);
}
const user = await auth0Client.getUser();
setUser(user);
setIsAuthenticated(true);
};
Apparently that works, I can still log in etc.
But upon close inspection, two things occur:
- the code after
await auth0Client.loginWithRedirect(params)
runs immediately (i.e., promise resolves) - when the user is redirected, the app loses state, so that code doesn’t even matter really
I have no problem with that, since after logging in and being redirected back to the app, init()
takes care of things smoothly.
Now comes the thread title. Why would I await a promise that immediately resolves void AND kills the app? The only reason I can think of is to catch Errors. Fair enough.
Am I missing something or is that sample code from the start of the post just wrong?