Skip to content

Commit c3ad953

Browse files
committed
Remove redirect to task within withRedirect guard
`withAfterToSignIn` and `withAfterToSignUp` guards were redirecting to a tasks URL with a warning: "Cannot render SignIn/SignUp if there's an existing task" This is wrong, cause SignIn/SignUp are rendering, but they should go to their initial route as tasks instead. If you try to access `/sign-in`, then the component should navigate to `/tasks` A follow-up commit will introducer a proper fix.
1 parent cd73364 commit c3ad953

File tree

10 files changed

+32
-86
lines changed

10 files changed

+32
-86
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ import {
110110
isError,
111111
isOrganizationId,
112112
isRedirectForFAPIInitiatedFlow,
113+
isSignedInAndSingleSessionModeEnabled,
113114
noOrganizationExists,
114115
noUserExists,
115116
processCssLayerNameExtraction,
116117
removeClerkQueryParam,
117118
requiresUserInput,
118-
sessionExistsAndSingleSessionModeEnabled,
119119
stripOrigin,
120120
windowNavigate,
121121
} from '../utils';
@@ -551,7 +551,7 @@ export class Clerk implements ClerkInterface {
551551

552552
public openSignIn = (props?: SignInProps): void => {
553553
this.assertComponentsReady(this.#componentControls);
554-
if (sessionExistsAndSingleSessionModeEnabled(this, this.environment)) {
554+
if (isSignedInAndSingleSessionModeEnabled(this, this.environment)) {
555555
if (this.#instanceType === 'development') {
556556
throw new ClerkRuntimeError(warnings.cannotOpenSignInOrSignUp, {
557557
code: CANNOT_RENDER_SINGLE_SESSION_ENABLED_ERROR_CODE,
@@ -683,7 +683,7 @@ export class Clerk implements ClerkInterface {
683683

684684
public openSignUp = (props?: SignUpProps): void => {
685685
this.assertComponentsReady(this.#componentControls);
686-
if (sessionExistsAndSingleSessionModeEnabled(this, this.environment)) {
686+
if (isSignedInAndSingleSessionModeEnabled(this, this.environment)) {
687687
if (this.#instanceType === 'development') {
688688
throw new ClerkRuntimeError(warnings.cannotOpenSignInOrSignUp, {
689689
code: CANNOT_RENDER_SINGLE_SESSION_ENABLED_ERROR_CODE,
@@ -1257,6 +1257,8 @@ export class Clerk implements ClerkInterface {
12571257
}
12581258
}
12591259

1260+
debugger;
1261+
12601262
if (newSession?.status === 'pending') {
12611263
await this.#handlePendingSession(newSession, onPendingSession);
12621264
return;
@@ -1329,6 +1331,8 @@ export class Clerk implements ClerkInterface {
13291331
};
13301332

13311333
#handlePendingSession = async (session: PendingSessionResource, onPendingSession?: OnPendingSessionFn) => {
1334+
debugger;
1335+
13321336
if (!this.environment) {
13331337
return;
13341338
}
@@ -1351,11 +1355,12 @@ export class Clerk implements ClerkInterface {
13511355

13521356
if (currentSession.status === 'pending') {
13531357
const tracker = createBeforeUnloadTracker(this.#options.standardBrowser);
1354-
const onPendingSessionHook = this.__internal_getOption('onPendingSession') ?? onPendingSession;
1355-
const taskUrls = this.__internal_getOption('taskUrls');
1358+
const onPendingSessionHook = this.#options['onPendingSession'] ?? onPendingSession;
1359+
const taskUrls = this.#options['taskUrls'];
13561360

13571361
await tracker.track(async () => {
13581362
if (onPendingSessionHook) {
1363+
debugger;
13591364
await onPendingSessionHook({ session: currentSession });
13601365
} else if (taskUrls) {
13611366
await this.navigate(taskUrls[session.currentTask.key]);

packages/clerk-js/src/ui/common/redirects.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import type { ClerkOptions, SessionTask } from '@clerk/types';
2-
3-
import { INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '../../core/sessionTasks';
41
import { buildURL } from '../../utils/url';
52
import type { SignInContextType, SignUpContextType, UserProfileContextType } from './../contexts';
63

@@ -28,33 +25,6 @@ export function buildVerificationRedirectUrl({
2825
});
2926
}
3027

31-
export function buildSessionTaskRedirectUrl({
32-
routing,
33-
path,
34-
baseUrl,
35-
task,
36-
taskUrls,
37-
}: Pick<SignInContextType | SignUpContextType, 'routing' | 'path'> & {
38-
baseUrl: string;
39-
task?: SessionTask;
40-
taskUrls?: ClerkOptions['taskUrls'];
41-
}) {
42-
if (!task) {
43-
return null;
44-
}
45-
46-
return (
47-
taskUrls?.[task.key] ??
48-
buildRedirectUrl({
49-
routing,
50-
baseUrl,
51-
path,
52-
endpoint: `/tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[task.key]}`,
53-
authQueryString: null,
54-
})
55-
);
56-
}
57-
5828
export function buildSSOCallbackURL(
5929
ctx: Partial<SignInContextType | SignUpContextType>,
6030
baseUrl: string | undefined = '',

packages/clerk-js/src/ui/common/withRedirect.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React from 'react';
66

77
import { warnings } from '../../core/warnings';
88
import type { ComponentGuard } from '../../utils';
9-
import { sessionExistsAndSingleSessionModeEnabled } from '../../utils';
9+
import { isSignedInAndSingleSessionModeEnabled } from '../../utils';
1010
import { useEnvironment, useOptions, useSignInContext, useSignUpContext } from '../contexts';
1111
import { useRouter } from '../router';
1212
import type { AvailableComponentProps } from '../types';
@@ -60,11 +60,9 @@ export const withRedirectToAfterSignIn = <P extends AvailableComponentProps>(Com
6060
const signInCtx = useSignInContext();
6161
return withRedirect(
6262
Component,
63-
sessionExistsAndSingleSessionModeEnabled,
64-
({ clerk }) => signInCtx.sessionTaskUrl || signInCtx.afterSignInUrl || clerk.buildAfterSignInUrl(),
65-
signInCtx.sessionTaskUrl
66-
? warnings.cannotRenderSignInComponentWhenTaskExists
67-
: warnings.cannotRenderSignInComponentWhenSessionExists,
63+
isSignedInAndSingleSessionModeEnabled,
64+
({ clerk }) => signInCtx.afterSignInUrl || clerk.buildAfterSignInUrl(),
65+
warnings.cannotRenderSignInComponentWhenSessionExists,
6866
)(props);
6967
};
7068

@@ -81,11 +79,9 @@ export const withRedirectToAfterSignUp = <P extends AvailableComponentProps>(Com
8179
const signUpCtx = useSignUpContext();
8280
return withRedirect(
8381
Component,
84-
sessionExistsAndSingleSessionModeEnabled,
85-
({ clerk }) => signUpCtx.sessionTaskUrl || signUpCtx.afterSignUpUrl || clerk.buildAfterSignUpUrl(),
86-
signUpCtx.sessionTaskUrl
87-
? warnings.cannotRenderSignUpComponentWhenTaskExists
88-
: warnings.cannotRenderSignUpComponentWhenSessionExists,
82+
isSignedInAndSingleSessionModeEnabled,
83+
({ clerk }) => signUpCtx.afterSignUpUrl || clerk.buildAfterSignUpUrl(),
84+
warnings.cannotRenderSignUpComponentWhenSessionExists,
8985
)(props);
9086
};
9187

packages/clerk-js/src/ui/components/SessionTasks/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const SessionTasksStart = () => {
3939
);
4040
};
4141

42-
function SessionTaskRoutes(): JSX.Element {
42+
function SessionTasksRoutes(): JSX.Element {
4343
const ctx = useSessionTasksContext();
4444

4545
return (
@@ -61,7 +61,7 @@ function SessionTaskRoutes(): JSX.Element {
6161
/**
6262
* @internal
6363
*/
64-
export const SessionTask = withCardStateProvider(() => {
64+
export const SessionTasks = withCardStateProvider(() => {
6565
const clerk = useClerk();
6666
const { navigate } = useRouter();
6767
const signInContext = useContext(SignInContext);
@@ -104,7 +104,7 @@ export const SessionTask = withCardStateProvider(() => {
104104

105105
return (
106106
<SessionTasksContext.Provider value={{ redirectUrlComplete, currentTaskContainer }}>
107-
<SessionTaskRoutes />
107+
<SessionTasksRoutes />
108108
</SessionTasksContext.Provider>
109109
);
110110
});

packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ function SignInStartInternal(): JSX.Element {
198198
}
199199
}, [identifierField.value, identifierAttributes]);
200200

201+
// TODO -> Maybe navigate on a effect here?
201202
useEffect(() => {
202203
if (!organizationTicket) {
203204
return;

packages/clerk-js/src/ui/components/SignUp/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { SessionTasks as LazySessionTasks } from '../../../ui/lazyModules/compon
66
import { SignUpEmailLinkFlowComplete } from '../../common/EmailLinkCompleteFlowCard';
77
import { SignUpContext, useSignUpContext, withCoreSessionSwitchGuard } from '../../contexts';
88
import { Flow } from '../../customizables';
9-
import { usePreloadTasks } from '../../hooks/usePreloadTasks';
109
import { Route, Switch, VIRTUAL_ROUTER_BASE_PATH } from '../../router';
1110
import { SignUpContinue } from './SignUpContinue';
1211
import { SignUpSSOCallback } from './SignUpSSOCallback';
@@ -23,8 +22,6 @@ function RedirectToSignUp() {
2322
}
2423

2524
function SignUpRoutes(): JSX.Element {
26-
usePreloadTasks();
27-
2825
const signUpContext = useSignUpContext();
2926

3027
return (

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

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

6+
import { navigateToTask } from '@/core/sessionTasks';
7+
68
import { SIGN_IN_INITIAL_VALUE_KEYS } from '../../../core/constants';
79
import { buildURL } from '../../../utils';
810
import { RedirectUrls } from '../../../utils/redirectUrls';
9-
import {
10-
buildRedirectUrl,
11-
buildSessionTaskRedirectUrl,
12-
MAGIC_LINK_VERIFY_PATH_ROUTE,
13-
SSO_CALLBACK_PATH_ROUTE,
14-
} from '../../common/redirects';
11+
import { buildRedirectUrl, MAGIC_LINK_VERIFY_PATH_ROUTE, SSO_CALLBACK_PATH_ROUTE } from '../../common/redirects';
1512
import { useEnvironment, useOptions } from '../../contexts';
1613
import type { ParsedQueryString } from '../../router';
1714
import { useRouter } from '../../router';
@@ -27,7 +24,6 @@ export type SignInContextType = Omit<SignInCtx, 'fallbackRedirectUrl' | 'forceRe
2724
authQueryString: string | null;
2825
afterSignUpUrl: string;
2926
afterSignInUrl: string;
30-
sessionTaskUrl: string | null;
3127
transferable: boolean;
3228
waitlistUrl: string;
3329
emailLinkRedirectUrl: string;
@@ -123,19 +119,14 @@ export const useSignInContext = (): SignInContextType => {
123119

124120
const signUpContinueUrl = buildURL({ base: signUpUrl, hashPath: '/continue' }, { stringify: true });
125121

126-
const sessionTaskUrl = buildSessionTaskRedirectUrl({
127-
task: clerk.session?.currentTask,
128-
path: ctx.path,
129-
routing: ctx.routing,
130-
baseUrl: signInUrl,
131-
taskUrls: clerk.__internal_getOption('taskUrls'),
132-
});
133-
134122
const onPendingSession: OnPendingSessionFn = async ({ session }) => {
135123
switch (session.currentTask?.key) {
136124
case 'choose-organization': {
137125
// TODO - preserve redirect_url
138-
await navigate(buildURL({ base: signInUrl, hashPath: '/tasks/choose-organization' }, { stringify: true }));
126+
await navigateToTask(session, {
127+
navigate,
128+
baseUrl: signInUrl,
129+
});
139130
}
140131
}
141132
};
@@ -152,7 +143,6 @@ export const useSignInContext = (): SignInContextType => {
152143
afterSignUpUrl,
153144
emailLinkRedirectUrl,
154145
ssoCallbackUrl,
155-
sessionTaskUrl,
156146
navigateAfterSignIn,
157147
signUpContinueUrl,
158148
queryParams,

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import { createContext, useContext, useMemo } from 'react';
55
import { SIGN_UP_INITIAL_VALUE_KEYS } from '../../../core/constants';
66
import { buildURL } from '../../../utils';
77
import { RedirectUrls } from '../../../utils/redirectUrls';
8-
import {
9-
buildRedirectUrl,
10-
buildSessionTaskRedirectUrl,
11-
MAGIC_LINK_VERIFY_PATH_ROUTE,
12-
SSO_CALLBACK_PATH_ROUTE,
13-
} from '../../common/redirects';
8+
import { buildRedirectUrl, MAGIC_LINK_VERIFY_PATH_ROUTE, SSO_CALLBACK_PATH_ROUTE } from '../../common/redirects';
149
import { useEnvironment, useOptions } from '../../contexts';
1510
import type { ParsedQueryString } from '../../router';
1611
import { useRouter } from '../../router';
@@ -116,14 +111,6 @@ export const useSignUpContext = (): SignUpContextType => {
116111
// TODO: Avoid building this url again to remove duplicate code. Get it from window.Clerk instead.
117112
const secondFactorUrl = buildURL({ base: signInUrl, hashPath: '/factor-two' }, { stringify: true });
118113

119-
const sessionTaskUrl = buildSessionTaskRedirectUrl({
120-
task: clerk.session?.currentTask,
121-
path: ctx.path,
122-
routing: ctx.routing,
123-
baseUrl: signUpUrl,
124-
taskUrls: clerk.__internal_getOption('taskUrls'),
125-
});
126-
127114
return {
128115
...ctx,
129116
oauthFlow: ctx.oauthFlow || 'auto',

packages/clerk-js/src/ui/lazyModules/components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export const OAuthConsent = lazy(() =>
122122
);
123123

124124
export const SessionTasks = lazy(() =>
125-
componentImportPaths.SessionTasks().then(module => ({ default: module.SessionTask })),
125+
componentImportPaths.SessionTasks().then(module => ({ default: module.SessionTasks })),
126126
);
127127

128128
export const preloadComponent = async (component: unknown) => {

packages/clerk-js/src/utils/componentGuards.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export type ComponentGuard = (
66
options?: ClerkOptions,
77
) => boolean;
88

9-
export const sessionExistsAndSingleSessionModeEnabled: ComponentGuard = (clerk, environment) => {
10-
return !!(clerk.session && environment?.authConfig.singleSessionMode);
9+
export const isSignedInAndSingleSessionModeEnabled: ComponentGuard = (clerk, environment) => {
10+
return !!(clerk.isSignedIn && environment?.authConfig.singleSessionMode);
1111
};
1212

1313
export const noUserExists: ComponentGuard = clerk => {

0 commit comments

Comments
 (0)