From d2c46edd8a445d6d68cf51b013e887bbc033e4ed Mon Sep 17 00:00:00 2001 From: Walter Berggren Date: Sat, 1 Mar 2025 02:10:42 +0200 Subject: [PATCH] Call onRedirectCallback also when logging in fails --- __tests__/auth-provider.test.tsx | 3 ++- src/auth0-provider.tsx | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/__tests__/auth-provider.test.tsx b/__tests__/auth-provider.test.tsx index 0ec66d3c..f6d170be 100644 --- a/__tests__/auth-provider.test.tsx +++ b/__tests__/auth-provider.test.tsx @@ -225,7 +225,7 @@ describe('Auth0Provider', () => { }); }); - it('should handle redirect callback errors', async () => { + it('should handle redirect callback errors and clear the url', async () => { window.history.pushState( {}, document.title, @@ -244,6 +244,7 @@ describe('Auth0Provider', () => { expect(() => { throw result.current.error; }).toThrowError('__test_error__'); + expect(window.location.href).toBe('https://www.example.com/'); }); }); diff --git a/src/auth0-provider.tsx b/src/auth0-provider.tsx index 9e4e09a2..0e7e8ab3 100644 --- a/src/auth0-provider.tsx +++ b/src/auth0-provider.tsx @@ -157,12 +157,12 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => { } didInitialise.current = true; (async (): Promise => { + let appState: AppState | undefined + let user: User | undefined; try { - let user: User | undefined; if (hasAuthParams() && !skipRedirectCallback) { - const { appState } = await client.handleRedirectCallback(); + appState = (await client.handleRedirectCallback()).appState; user = await client.getUser(); - onRedirectCallback(appState, user); } else { await client.checkSession(); user = await client.getUser(); @@ -170,6 +170,8 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => { dispatch({ type: 'INITIALISED', user }); } catch (error) { handleError(loginError(error)); + } finally { + onRedirectCallback(appState, user); } })(); }, [client, onRedirectCallback, skipRedirectCallback, handleError]);