Skip to content

Commit eae9575

Browse files
committed
Rename RedirectToTask control component to RedirectToTasks
1 parent df77859 commit eae9575

File tree

15 files changed

+121
-24
lines changed

15 files changed

+121
-24
lines changed

.changeset/warm-rocks-flow.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
---
22
'@clerk/tanstack-react-start': minor
33
'@clerk/react-router': minor
4-
'@clerk/clerk-js': minor
54
'@clerk/nextjs': minor
65
'@clerk/clerk-react': minor
76
'@clerk/remix': minor
87
'@clerk/types': minor
98
'@clerk/vue': minor
109
---
1110

12-
Remove `RedirectToTask` control component
11+
Rename `RedirectToTask` control component to `RedirectToTasks`

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ import {
151151
Organization,
152152
Waitlist,
153153
} from './resources/internal';
154-
import { navigateIfTaskExists, warnMissingPendingTaskHandlers } from './sessionTasks';
154+
import {
155+
INTERNAL_SESSION_TASK_ROUTE_BY_KEY,
156+
navigateIfTaskExists,
157+
warnMissingPendingTaskHandlers,
158+
} from './sessionTasks';
155159
import { State } from './state';
156160
import { warnings } from './warnings';
157161

@@ -1560,6 +1564,28 @@ export class Clerk implements ClerkInterface {
15601564
return this.buildUrlWithAuth(this.environment.displayConfig.organizationProfileUrl);
15611565
}
15621566

1567+
public buildTasksUrl(): string {
1568+
const currentTask = this.session?.currentTask;
1569+
if (!currentTask) {
1570+
return '';
1571+
}
1572+
1573+
const customTaskUrl = this.#options.taskUrls?.[currentTask.key];
1574+
if (customTaskUrl) {
1575+
return customTaskUrl;
1576+
}
1577+
1578+
return buildURL(
1579+
{
1580+
base: this.buildSignInUrl(),
1581+
hashPath: `/tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[currentTask.key]}`,
1582+
},
1583+
{
1584+
stringify: true,
1585+
},
1586+
);
1587+
}
1588+
15631589
#redirectToSatellite = async (): Promise<unknown> => {
15641590
if (!inBrowser()) {
15651591
return;
@@ -1650,6 +1676,13 @@ export class Clerk implements ClerkInterface {
16501676
return;
16511677
};
16521678

1679+
public redirectToTasks = async (): Promise<unknown> => {
1680+
if (inBrowser()) {
1681+
return this.navigate(this.buildTasksUrl());
1682+
}
1683+
return;
1684+
};
1685+
16531686
public handleEmailLinkVerification = async (
16541687
params: HandleEmailLinkVerificationParams,
16551688
customNavigate?: (to: string) => Promise<unknown>,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const INTERNAL_SESSION_TASK_ROUTE_BY_KEY: Record<SessionTask['key'], stri
1313
/**
1414
* @internal
1515
*/
16-
export function buildTaskURL(task: SessionTask, opts: Pick<Parameters<typeof buildURL>[0], 'base'>) {
16+
export function buildTasksUrl(task: SessionTask, opts: Pick<Parameters<typeof buildURL>[0], 'base'>) {
1717
return buildURL(
1818
{
1919
base: opts.base,
@@ -41,7 +41,7 @@ export function navigateIfTaskExists(
4141
return;
4242
}
4343

44-
return navigate(buildTaskURL(currentTask, { base: baseUrl }));
44+
return navigate(buildTasksUrl(currentTask, { base: baseUrl }));
4545
}
4646

4747
export function warnMissingPendingTaskHandlers(options: Record<string, unknown>) {

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { isClerkRuntimeError, isUserLockedError } from '@clerk/shared/error';
22
import { useClerk } from '@clerk/shared/react';
33
import { useCallback, useEffect } from 'react';
44

5-
import { navigateIfTaskExists } from '@/core/sessionTasks';
65
import { useCardState } from '@/ui/elements/contexts';
76
import { useRouter } from '@/ui/router';
87
import { handleError } from '@/ui/utils/errorHandler';
@@ -17,7 +16,7 @@ function useHandleAuthenticateWithPasskey(onSecondFactor: () => Promise<unknown>
1716
// @ts-expect-error -- private method for the time being
1817
const { setActive, __internal_navigateWithError } = useClerk();
1918
const supportEmail = useSupportEmail();
20-
const { afterSignInUrl, signInUrl } = useSignInContext();
19+
const { afterSignInUrl, signInUrl, navigateOnSetActive } = useSignInContext();
2120
const { authenticateWithPasskey } = useCoreSignIn();
2221
const { navigate } = useRouter();
2322

@@ -35,15 +34,7 @@ function useHandleAuthenticateWithPasskey(onSecondFactor: () => Promise<unknown>
3534
return setActive({
3635
session: res.createdSessionId,
3736
navigate: async ({ session }) => {
38-
if (!session.currentTask) {
39-
await navigate(afterSignInUrl);
40-
return;
41-
}
42-
43-
await navigateIfTaskExists(session, {
44-
baseUrl: signInUrl,
45-
navigate: navigate,
46-
});
37+
await navigateOnSetActive({ session, redirectUrl: afterSignInUrl });
4738
},
4839
});
4940
case 'needs_second_factor':

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

Lines changed: 4 additions & 4 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 { buildTasksUrl, 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';
@@ -37,7 +37,7 @@ export const SignInContext = createContext<SignInCtx | null>(null);
3737

3838
export const useSignInContext = (): SignInContextType => {
3939
const context = useContext(SignInContext);
40-
const { navigate } = useRouter();
40+
const { navigate, indexPath } = useRouter();
4141
const { displayConfig, userSettings } = useEnvironment();
4242
const { queryParams, queryString } = useRouter();
4343
const signUpMode = userSettings.signUp.mode;
@@ -126,12 +126,12 @@ export const useSignInContext = (): SignInContextType => {
126126
return navigate(redirectUrl);
127127
}
128128

129-
return navigate(`../tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[currentTask.key]}`);
129+
return navigate(indexPath + `/tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[currentTask.key]}`);
130130
};
131131

132132
const taskUrl = clerk.session?.currentTask
133133
? (clerk.__internal_getOption('taskUrls')?.[clerk.session?.currentTask.key] ??
134-
buildTaskURL(clerk.session?.currentTask, { base: signInUrl }))
134+
buildTasksUrl(clerk.session?.currentTask, { base: signInUrl }))
135135
: null;
136136

137137
return {

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

Lines changed: 4 additions & 4 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 { buildTasksUrl, 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';
@@ -36,7 +36,7 @@ export const SignUpContext = createContext<SignUpCtx | null>(null);
3636

3737
export const useSignUpContext = (): SignUpContextType => {
3838
const context = useContext(SignUpContext);
39-
const { navigate } = useRouter();
39+
const { navigate, indexPath } = useRouter();
4040
const { displayConfig, userSettings } = useEnvironment();
4141
const { queryParams, queryString } = useRouter();
4242
const signUpMode = userSettings.signUp.mode;
@@ -121,12 +121,12 @@ export const useSignUpContext = (): SignUpContextType => {
121121
return navigate(redirectUrl);
122122
}
123123

124-
return navigate(`../tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[currentTask.key]}`);
124+
return navigate(indexPath + `/tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[currentTask.key]}`);
125125
};
126126

127127
const taskUrl = clerk.session?.currentTask
128128
? (clerk.__internal_getOption('taskUrls')?.[clerk.session?.currentTask.key] ??
129-
buildTaskURL(clerk.session?.currentTask, { base: signUpUrl }))
129+
buildTasksUrl(clerk.session?.currentTask, { base: signUpUrl }))
130130
: null;
131131

132132
return {

packages/nextjs/src/client-boundary/controlComponents.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
RedirectToOrganizationProfile,
1212
RedirectToSignIn,
1313
RedirectToSignUp,
14+
RedirectToTasks,
1415
RedirectToUserProfile,
1516
SignedIn,
1617
SignedOut,

packages/react-router/src/__tests__/__snapshots__/exports.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exports[`root public exports > should not change unexpectedly 1`] = `
2020
"RedirectToOrganizationProfile",
2121
"RedirectToSignIn",
2222
"RedirectToSignUp",
23+
"RedirectToTasks",
2324
"RedirectToUserProfile",
2425
"SignIn",
2526
"SignInButton",

packages/react/src/components/controlComponents.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ export const RedirectToSignUp = withClerk(({ clerk, ...props }: WithClerkProp<Re
166166
return null;
167167
}, 'RedirectToSignUp');
168168

169+
export const RedirectToTasks = withClerk(({ clerk }: WithClerkProp) => {
170+
const { session } = clerk;
171+
172+
React.useEffect(() => {
173+
if (!session) {
174+
void clerk.redirectToSignIn();
175+
return;
176+
}
177+
178+
if (!session.currentTask) {
179+
void clerk.redirectToAfterSignIn();
180+
return;
181+
}
182+
183+
void clerk.redirectToTasks();
184+
}, []);
185+
186+
return null;
187+
}, 'RedirectToTasks');
188+
169189
/**
170190
* @function
171191
* @deprecated Use [`redirectToUserProfile()`](https://clerk.com/docs/references/javascript/clerk#redirect-to-user-profile) instead.

packages/react/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525
RedirectToOrganizationProfile,
2626
RedirectToSignIn,
2727
RedirectToSignUp,
28+
RedirectToTasks,
2829
RedirectToUserProfile,
2930
SignedIn,
3031
SignedOut,

0 commit comments

Comments
 (0)