diff --git a/src/core/interfaces/IWebAuthProvider.ts b/src/core/interfaces/IWebAuthProvider.ts index e2d58696..24c17d5f 100644 --- a/src/core/interfaces/IWebAuthProvider.ts +++ b/src/core/interfaces/IWebAuthProvider.ts @@ -54,10 +54,20 @@ export interface IWebAuthProvider { options?: NativeClearSessionOptions | WebClearSessionOptions ): Promise; + /** + * Retrives the authenticated user's profile information. + * + * @remarks + * This method fetches the user's profile from the Auth0 session if available. + * + * @returns A promise that resolves with the user's profile information, or null if not authenticated. + */ + getWebUser(): Promise; + /** * Checks the user's session and updates the local state if the session is still valid. */ - checkWebSession(): Promise; + checkWebSession(): Promise; /** * Cancels an ongoing web authentication transaction. diff --git a/src/hooks/Auth0Provider.tsx b/src/hooks/Auth0Provider.tsx index 46231f63..44b44b86 100644 --- a/src/hooks/Auth0Provider.tsx +++ b/src/hooks/Auth0Provider.tsx @@ -55,6 +55,7 @@ export const Auth0Provider = ({ window?.location?.search?.includes('state='); if (hasRedirectParams) { try { + user = await client.webAuth.getWebUser(); // If it does, handle the redirect. This will exchange the code for tokens. await client.webAuth.handleRedirectCallback(); // Clean the URL @@ -68,7 +69,8 @@ export const Auth0Provider = ({ dispatch({ type: 'ERROR', error: e as AuthError }); } } else if (typeof window !== 'undefined') { - user = await client.webAuth.checkWebSession(); + await client.webAuth.checkWebSession(); + user = await client.webAuth.getWebUser(); } } else if (await client.credentialsManager.hasValidCredentials()) { try { diff --git a/src/platforms/native/adapters/NativeWebAuthProvider.ts b/src/platforms/native/adapters/NativeWebAuthProvider.ts index f26054bb..23b47919 100644 --- a/src/platforms/native/adapters/NativeWebAuthProvider.ts +++ b/src/platforms/native/adapters/NativeWebAuthProvider.ts @@ -29,7 +29,11 @@ export class NativeWebAuthProvider implements IWebAuthProvider { throw new AuthError('NotImplemented', webAuthNotSupported); } - async checkWebSession(): Promise { + async checkWebSession() { + throw new AuthError('NotImplemented', webAuthNotSupported); + } + + async getWebUser(): Promise { throw new AuthError('NotImplemented', webAuthNotSupported); } diff --git a/src/platforms/web/adapters/WebWebAuthProvider.ts b/src/platforms/web/adapters/WebWebAuthProvider.ts index 02d4bf85..4082d991 100644 --- a/src/platforms/web/adapters/WebWebAuthProvider.ts +++ b/src/platforms/web/adapters/WebWebAuthProvider.ts @@ -86,7 +86,7 @@ export class WebWebAuthProvider implements IWebAuthProvider { await this.client.logout({ logoutParams: { federated: parameters.federated, - returnTo: parameters.returnToUrl, + returnTo: parameters.returnToUrl || window?.location?.origin, }, }); } catch (e: any) { @@ -111,14 +111,17 @@ export class WebWebAuthProvider implements IWebAuthProvider { } } - async checkWebSession(): Promise { - await this.client.checkSession(); + async getWebUser(): Promise { const spaUser: SpaJSUser | undefined = await this.client.getUser(); // convert this to a User const user = this.convertUser(spaUser); return user; } + async checkWebSession() { + await this.client.checkSession(); + } + async cancelWebAuth(): Promise { // Web-based flows cannot be programmatically cancelled. This is a no-op. return Promise.resolve(); diff --git a/src/platforms/web/adapters/__tests__/WebWebAuthProvider.spec.ts b/src/platforms/web/adapters/__tests__/WebWebAuthProvider.spec.ts index 7cbd14d9..524f9cd3 100644 --- a/src/platforms/web/adapters/__tests__/WebWebAuthProvider.spec.ts +++ b/src/platforms/web/adapters/__tests__/WebWebAuthProvider.spec.ts @@ -193,7 +193,7 @@ describe('WebWebAuthProvider', () => { expect(mockSpaClient.logout).toHaveBeenCalledWith({ logoutParams: { - returnTo: undefined, + returnTo: 'http://localhost', federated: undefined, }, }); @@ -204,7 +204,7 @@ describe('WebWebAuthProvider', () => { expect(mockSpaClient.logout).toHaveBeenCalledWith({ logoutParams: { - returnTo: undefined, + returnTo: 'http://localhost', federated: undefined, }, });