Skip to content

Commit 1d296dd

Browse files
fix: reload and added default redirect url after login for web (#1326)
1 parent 3432f06 commit 1d296dd

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

src/core/interfaces/IWebAuthProvider.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,20 @@ export interface IWebAuthProvider {
5454
options?: NativeClearSessionOptions | WebClearSessionOptions
5555
): Promise<void>;
5656

57+
/**
58+
* Retrives the authenticated user's profile information.
59+
*
60+
* @remarks
61+
* This method fetches the user's profile from the Auth0 session if available.
62+
*
63+
* @returns A promise that resolves with the user's profile information, or null if not authenticated.
64+
*/
65+
getWebUser(): Promise<User | null>;
66+
5767
/**
5868
* Checks the user's session and updates the local state if the session is still valid.
5969
*/
60-
checkWebSession(): Promise<User | null>;
70+
checkWebSession(): Promise<void>;
6171

6272
/**
6373
* Cancels an ongoing web authentication transaction.

src/hooks/Auth0Provider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const Auth0Provider = ({
5555
window?.location?.search?.includes('state=');
5656
if (hasRedirectParams) {
5757
try {
58+
user = await client.webAuth.getWebUser();
5859
// If it does, handle the redirect. This will exchange the code for tokens.
5960
await client.webAuth.handleRedirectCallback();
6061
// Clean the URL
@@ -68,7 +69,8 @@ export const Auth0Provider = ({
6869
dispatch({ type: 'ERROR', error: e as AuthError });
6970
}
7071
} else if (typeof window !== 'undefined') {
71-
user = await client.webAuth.checkWebSession();
72+
await client.webAuth.checkWebSession();
73+
user = await client.webAuth.getWebUser();
7274
}
7375
} else if (await client.credentialsManager.hasValidCredentials()) {
7476
try {

src/platforms/native/adapters/NativeWebAuthProvider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export class NativeWebAuthProvider implements IWebAuthProvider {
2929
throw new AuthError('NotImplemented', webAuthNotSupported);
3030
}
3131

32-
async checkWebSession(): Promise<User | null> {
32+
async checkWebSession() {
33+
throw new AuthError('NotImplemented', webAuthNotSupported);
34+
}
35+
36+
async getWebUser(): Promise<User | null> {
3337
throw new AuthError('NotImplemented', webAuthNotSupported);
3438
}
3539

src/platforms/web/adapters/WebWebAuthProvider.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class WebWebAuthProvider implements IWebAuthProvider {
8686
await this.client.logout({
8787
logoutParams: {
8888
federated: parameters.federated,
89-
returnTo: parameters.returnToUrl,
89+
returnTo: parameters.returnToUrl || window?.location?.origin,
9090
},
9191
});
9292
} catch (e: any) {
@@ -111,14 +111,17 @@ export class WebWebAuthProvider implements IWebAuthProvider {
111111
}
112112
}
113113

114-
async checkWebSession(): Promise<User | null> {
115-
await this.client.checkSession();
114+
async getWebUser(): Promise<User | null> {
116115
const spaUser: SpaJSUser | undefined = await this.client.getUser();
117116
// convert this to a User
118117
const user = this.convertUser(spaUser);
119118
return user;
120119
}
121120

121+
async checkWebSession() {
122+
await this.client.checkSession();
123+
}
124+
122125
async cancelWebAuth(): Promise<void> {
123126
// Web-based flows cannot be programmatically cancelled. This is a no-op.
124127
return Promise.resolve();

src/platforms/web/adapters/__tests__/WebWebAuthProvider.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('WebWebAuthProvider', () => {
193193

194194
expect(mockSpaClient.logout).toHaveBeenCalledWith({
195195
logoutParams: {
196-
returnTo: undefined,
196+
returnTo: 'http://localhost',
197197
federated: undefined,
198198
},
199199
});
@@ -204,7 +204,7 @@ describe('WebWebAuthProvider', () => {
204204

205205
expect(mockSpaClient.logout).toHaveBeenCalledWith({
206206
logoutParams: {
207-
returnTo: undefined,
207+
returnTo: 'http://localhost',
208208
federated: undefined,
209209
},
210210
});

0 commit comments

Comments
 (0)