Skip to content

Commit 5ad3f51

Browse files
committed
Fix navigation for SSO to maintain path and hash routing
1 parent f492e9a commit 5ad3f51

File tree

9 files changed

+28
-19
lines changed

9 files changed

+28
-19
lines changed

integration/tests/session-tasks-sign-in.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withSessionTasks] })(
8383
await u.po.signIn.enterTestOtpCode();
8484

8585
// Resolves task
86-
await u.po.signIn.waitForMounted();
86+
await u.po.signUp.waitForMounted();
8787
const fakeOrganization = Object.assign(u.services.organizations.createFakeOrganization(), {
8888
slug: u.services.organizations.createFakeOrganization().slug + '-with-sign-in-sso',
8989
});

integration/tests/session-tasks-sign-up.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withSessionTasks] })(
4141
});
4242
const users = createUserService(client);
4343
await users.deleteIfExists({ email: fakeUserForOAuth.email });
44-
// Delete OAuth user from `with-session-tasks` instance
45-
await u.services.users.deleteIfExists({ email: fakeUserForOAuth.email });
4644

4745
await app.teardown();
4846
});

packages/clerk-js/src/core/__tests__/clerk.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ describe('Clerk singleton', () => {
10331033
await sut.handleRedirectCallback();
10341034

10351035
await waitFor(() => {
1036-
expect(mockNavigate.mock.calls[0][0]).toBe('/sign-in#/tasks/choose-organization');
1036+
expect(mockNavigate.mock.calls[0][0]).toBe('/sign-up#/tasks/choose-organization');
10371037
});
10381038
});
10391039
});

packages/clerk-js/src/core/clerk.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import type {
2828
AuthenticateWithGoogleOneTapParams,
2929
AuthenticateWithMetamaskParams,
3030
AuthenticateWithOKXWalletParams,
31+
Clerk as ClerkInterface,
3132
ClerkAPIError,
3233
ClerkAuthenticateWithWeb3Params,
33-
Clerk as ClerkInterface,
3434
ClerkOptions,
3535
ClientJSONSnapshot,
3636
ClientResource,
@@ -1755,8 +1755,6 @@ export class Clerk implements ClerkInterface {
17551755
navigate: (to: string) => Promise<unknown>;
17561756
},
17571757
): Promise<unknown> => {
1758-
debugger;
1759-
17601758
if (!this.loaded || !this.environment || !this.client) {
17611759
return;
17621760
}
@@ -2006,10 +2004,7 @@ export class Clerk implements ClerkInterface {
20062004
}
20072005

20082006
if (this.session?.currentTask) {
2009-
await navigateIfTaskExists(this.session, {
2010-
baseUrl: params.signInUrl ?? displayConfig.signInUrl,
2011-
navigate: this.navigate,
2012-
});
2007+
await this.redirectToTasks();
20132008
return;
20142009
}
20152010

packages/clerk-js/src/core/sessionTasks.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export const INTERNAL_SESSION_TASK_ROUTE_BY_KEY: Record<SessionTask['key'], stri
1010
'choose-organization': 'choose-organization',
1111
} as const;
1212

13+
/**
14+
* @internal
15+
*/
16+
export const getTaskEndpoint = (task: SessionTask) => `/tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[task.key]}`;
17+
1318
/**
1419
* @internal
1520
*/

packages/clerk-js/src/ui/components/SessionTasks/tasks/withTaskGuard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import type { ComponentType } from 'react';
22

33
import { warnings } from '@/core/warnings';
44
import { withRedirect } from '@/ui/common';
5-
import { useSessionTasksContext } from '@/ui/contexts/components/SessionTasks';
5+
import { useTaskChooseOrganizationContext } from '@/ui/contexts/components/SessionTasks';
66
import type { AvailableComponentProps } from '@/ui/types';
77

88
export const withTaskGuard = <P extends AvailableComponentProps>(Component: ComponentType<P>) => {
99
const displayName = Component.displayName || Component.name || 'Component';
1010
Component.displayName = displayName;
1111

1212
const HOC = (props: P) => {
13-
const ctx = useSessionTasksContext();
13+
const ctx = useTaskChooseOrganizationContext();
1414
return withRedirect(
1515
Component,
1616
clerk => !clerk.session?.currentTask,

packages/clerk-js/src/ui/contexts/components/SignIn.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isAbsoluteUrl } from '@clerk/shared/url';
33
import type { SessionResource } from '@clerk/types';
44
import { createContext, useContext, useMemo } from 'react';
55

6-
import { buildTaskUrl, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '@/core/sessionTasks';
6+
import { getTaskEndpoint, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '@/core/sessionTasks';
77

88
import { SIGN_IN_INITIAL_VALUE_KEYS } from '../../../core/constants';
99
import { buildURL } from '../../../utils';
@@ -131,7 +131,13 @@ export const useSignInContext = (): SignInContextType => {
131131

132132
const taskUrl = clerk.session?.currentTask
133133
? (clerk.__internal_getOption('taskUrls')?.[clerk.session?.currentTask.key] ??
134-
buildTaskUrl(clerk.session?.currentTask, { base: signInUrl }))
134+
buildRedirectUrl({
135+
routing: ctx.routing,
136+
baseUrl: signInUrl,
137+
authQueryString,
138+
path: ctx.path,
139+
endpoint: getTaskEndpoint(clerk.session?.currentTask),
140+
}))
135141
: null;
136142

137143
return {

packages/clerk-js/src/ui/contexts/components/SignUp.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isAbsoluteUrl } from '@clerk/shared/url';
33
import type { SessionResource } from '@clerk/types';
44
import { createContext, useContext, useMemo } from 'react';
55

6-
import { buildTaskUrl, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '@/core/sessionTasks';
6+
import { getTaskEndpoint, INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '@/core/sessionTasks';
77

88
import { SIGN_UP_INITIAL_VALUE_KEYS } from '../../../core/constants';
99
import { buildURL } from '../../../utils';
@@ -126,7 +126,13 @@ export const useSignUpContext = (): SignUpContextType => {
126126

127127
const taskUrl = clerk.session?.currentTask
128128
? (clerk.__internal_getOption('taskUrls')?.[clerk.session?.currentTask.key] ??
129-
buildTaskUrl(clerk.session?.currentTask, { base: signUpUrl }))
129+
buildRedirectUrl({
130+
routing: ctx.routing,
131+
baseUrl: signInUrl,
132+
authQueryString,
133+
path: ctx.path,
134+
endpoint: getTaskEndpoint(clerk.session?.currentTask),
135+
}))
130136
: null;
131137

132138
return {

packages/types/src/clerk.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,7 @@ export type SetActiveParams = {
12081208
/**
12091209
* A custom navigation function to be called just before the session and/or organization is set.
12101210
*
1211-
* When provided, it takes precedence
1212-
* over the `redirectUrl` parameter for navigation.
1211+
* When provided, it takes precedence over the `redirectUrl` parameter for navigation.
12131212
*
12141213
* @example
12151214
* ```typescript

0 commit comments

Comments
 (0)