Skip to content

Commit c28d29c

Browse files
fix(clerk-js): Fix redirect when completing task within SignIn/SignUp (#6580)
Co-authored-by: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com>
1 parent e52bf8e commit c28d29c

File tree

12 files changed

+40
-26
lines changed

12 files changed

+40
-26
lines changed

.changeset/mean-pumas-cut.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
'@clerk/types': patch
4+
---
5+
6+
Fix incorrect redirect when completing session tasks within `SignIn` and `SignUp` components

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withSessionTasks] })(
5656
await u.po.expect.toHaveResolvedTask();
5757

5858
// Navigates to after sign-in
59-
await u.page.waitForAppUrl('/');
59+
await u.page.waitForAppUrl('/page-protected');
6060
});
6161

6262
test('with sso, navigate to task on after sign-in', async ({ page, context }) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withSessionTasks] })(
7373
await u.po.expect.toHaveResolvedTask();
7474

7575
// Navigates to after sign-up
76-
await u.page.waitForAppUrl('/');
76+
await u.page.waitForAppUrl('/page-protected');
7777
});
7878

7979
test('with sso, navigate to task on after sign-up', async ({ page, context }) => {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import type {
7575
SignUpRedirectOptions,
7676
SignUpResource,
7777
TaskChooseOrganizationProps,
78+
TasksRedirectOptions,
7879
UnsubscribeCallback,
7980
UserButtonProps,
8081
UserProfileProps,
@@ -1595,7 +1596,7 @@ export class Clerk implements ClerkInterface {
15951596
return this.buildUrlWithAuth(this.environment.displayConfig.organizationProfileUrl);
15961597
}
15971598

1598-
public buildTasksUrl(): string {
1599+
public buildTasksUrl(options?: TasksRedirectOptions): string {
15991600
const currentTask = this.session?.currentTask;
16001601
if (!currentTask) {
16011602
return '';
@@ -1608,7 +1609,7 @@ export class Clerk implements ClerkInterface {
16081609

16091610
return buildURL(
16101611
{
1611-
base: this.buildSignInUrl(),
1612+
base: this.buildSignInUrl(options),
16121613
hashPath: getTaskEndpoint(currentTask),
16131614
},
16141615
{
@@ -1707,9 +1708,9 @@ export class Clerk implements ClerkInterface {
17071708
return;
17081709
};
17091710

1710-
public redirectToTasks = async (): Promise<unknown> => {
1711+
public redirectToTasks = async (options?: TasksRedirectOptions): Promise<unknown> => {
17111712
if (inBrowser()) {
1712-
return this.navigate(this.buildTasksUrl());
1713+
return this.navigate(this.buildTasksUrl(options));
17131714
}
17141715
return;
17151716
};
@@ -2044,7 +2045,9 @@ export class Clerk implements ClerkInterface {
20442045
}
20452046

20462047
if (this.session?.currentTask) {
2047-
await this.redirectToTasks();
2048+
await this.redirectToTasks({
2049+
redirectUrl: this.buildAfterSignInUrl(),
2050+
});
20482051
return;
20492052
}
20502053

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { useClerk } from '@clerk/shared/react';
22
import { eventComponentMounted } from '@clerk/shared/telemetry';
33
import type { SessionResource } from '@clerk/types';
4-
import { useContext, useEffect, useRef } from 'react';
4+
import { useEffect, useRef } from 'react';
55

66
import { Card } from '@/ui/elements/Card';
77
import { withCardStateProvider } from '@/ui/elements/contexts';
88
import { LoadingCardContainer } from '@/ui/elements/LoadingCard';
99

1010
import { INTERNAL_SESSION_TASK_ROUTE_BY_KEY } from '../../../core/sessionTasks';
11-
import { SignInContext, SignUpContext } from '../../../ui/contexts';
1211
import {
1312
SessionTasksContext,
1413
TaskChooseOrganizationContext,
@@ -62,19 +61,18 @@ function SessionTasksRoutes(): JSX.Element {
6261
);
6362
}
6463

64+
type SessionTasksProps = {
65+
redirectUrlComplete: string;
66+
};
67+
6568
/**
6669
* @internal
6770
*/
68-
export const SessionTasks = withCardStateProvider(() => {
71+
export const SessionTasks = withCardStateProvider(({ redirectUrlComplete }: SessionTasksProps) => {
6972
const clerk = useClerk();
7073
const { navigate } = useRouter();
71-
const signInContext = useContext(SignInContext);
72-
const signUpContext = useContext(SignUpContext);
7374
const currentTaskContainer = useRef<HTMLDivElement>(null);
7475

75-
const redirectUrlComplete =
76-
signInContext?.afterSignInUrl ?? signUpContext?.afterSignUpUrl ?? clerk?.buildAfterSignInUrl();
77-
7876
// If there are no pending tasks, navigate away from the tasks flow.
7977
// This handles cases where a user with an active session returns to the tasks URL,
8078
// for example by using browser back navigation. Since there are no pending tasks,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ function SignInRoutes(): JSX.Element {
136136
</Route>
137137
</Route>
138138
<Route path='tasks'>
139-
<LazySessionTasks />
139+
<LazySessionTasks redirectUrlComplete={signInContext.afterSignUpUrl} />
140140
</Route>
141141
<Route index>
142142
<LazySignUpStart />
143143
</Route>
144144
</Route>
145145
)}
146146
<Route path='tasks'>
147-
<LazySessionTasks />
147+
<LazySessionTasks redirectUrlComplete={signInContext.afterSignInUrl} />
148148
</Route>
149149
<Route index>
150150
<SignInStart />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function SignUpRoutes(): JSX.Element {
8080
</Route>
8181
</Route>
8282
<Route path='tasks'>
83-
<LazySessionTasks />
83+
<LazySessionTasks redirectUrlComplete={signUpContext.afterSignUpUrl} />
8484
</Route>
8585
<Route index>
8686
<SignUpStart />

packages/react/src/components/controlComponents.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';
66
import { useSessionContext } from '../contexts/SessionContext';
77
import { useAuth } from '../hooks';
88
import { useAssertWrappedByClerkProvider } from '../hooks/useAssertWrappedByClerkProvider';
9-
import type { RedirectToSignInProps, RedirectToSignUpProps, WithClerkProp } from '../types';
9+
import type { RedirectToSignInProps, RedirectToSignUpProps, RedirectToTasksProps, WithClerkProp } from '../types';
1010
import { withClerk } from './withClerk';
1111

1212
export const SignedIn = ({ children, treatPendingAsSignedOut }: React.PropsWithChildren<PendingSessionOptions>) => {
@@ -166,9 +166,9 @@ export const RedirectToSignUp = withClerk(({ clerk, ...props }: WithClerkProp<Re
166166
return null;
167167
}, 'RedirectToSignUp');
168168

169-
export const RedirectToTasks = withClerk(({ clerk }: WithClerkProp) => {
169+
export const RedirectToTasks = withClerk(({ clerk, ...props }: WithClerkProp<RedirectToTasksProps>) => {
170170
React.useEffect(() => {
171-
void clerk.redirectToTasks();
171+
void clerk.redirectToTasks(props);
172172
}, []);
173173

174174
return null;

packages/react/src/isomorphicClerk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import type {
4545
SignUpResource,
4646
State,
4747
TaskChooseOrganizationProps,
48+
TasksRedirectOptions,
4849
UnsubscribeCallback,
4950
UserButtonProps,
5051
UserProfileProps,
@@ -1276,8 +1277,8 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
12761277
}
12771278
};
12781279

1279-
redirectToTasks = async () => {
1280-
const callback = () => this.clerkjs?.redirectToTasks();
1280+
redirectToTasks = async (opts?: TasksRedirectOptions) => {
1281+
const callback = () => this.clerkjs?.redirectToTasks(opts);
12811282
if (this.clerkjs && this.loaded) {
12821283
return callback();
12831284
} else {

packages/react/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
RedirectUrlProp,
1010
SignInRedirectOptions,
1111
SignUpRedirectOptions,
12+
TasksRedirectOptions,
1213
Without,
1314
} from '@clerk/types';
1415
import type React from 'react';
@@ -116,6 +117,7 @@ export type SignInWithMetamaskButtonProps = {
116117

117118
export type RedirectToSignInProps = SignInRedirectOptions;
118119
export type RedirectToSignUpProps = SignUpRedirectOptions;
120+
export type RedirectToTasksProps = TasksRedirectOptions;
119121

120122
type PageProps<T extends string> =
121123
| {

0 commit comments

Comments
 (0)