Skip to content

Commit 190f402

Browse files
committed
Improve redirection logic
1 parent 2112714 commit 190f402

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

packages/clerk-js/bundlewatch.config.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"files": [
3-
{ "path": "./dist/clerk.js", "maxSize": "610.32kB" },
4-
{ "path": "./dist/clerk.browser.js", "maxSize": "70.2KB" },
5-
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "113KB" },
3+
{ "path": "./dist/clerk.js", "maxSize": "612kB" },
4+
{ "path": "./dist/clerk.browser.js", "maxSize": "72.2KB" },
5+
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "115KB" },
66
{ "path": "./dist/clerk.headless*.js", "maxSize": "53.06KB" },
77
{ "path": "./dist/ui-common*.js", "maxSize": "108.75KB" },
88
{ "path": "./dist/vendors*.js", "maxSize": "40.2KB" },
@@ -25,6 +25,6 @@
2525
{ "path": "./dist/paymentSources*.js", "maxSize": "9.17KB" },
2626
{ "path": "./dist/up-billing-page*.js", "maxSize": "3.0KB" },
2727
{ "path": "./dist/op-billing-page*.js", "maxSize": "3.0KB" },
28-
{ "path": "./dist/sessionTasks*.js", "maxSize": "1KB" }
28+
{ "path": "./dist/sessionTasks*.js", "maxSize": "1.5KB" }
2929
]
3030
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useClerk } from '@clerk/shared/react';
22
import { eventComponentMounted } from '@clerk/shared/telemetry';
3-
import { useCallback, useContext, useEffect } from 'react';
3+
import { useCallback, useContext, useEffect, useState } from 'react';
44

55
import { Card } from '@/ui/elements/Card';
66
import { withCardStateProvider } from '@/ui/elements/contexts';
@@ -56,23 +56,34 @@ export const SessionTask = withCardStateProvider(() => {
5656
const { navigate } = useRouter();
5757
const signInContext = useContext(SignInContext);
5858
const signUpContext = useContext(SignUpContext);
59+
const [isNavigatingToTask, setIsNavigatingToTask] = useState(false);
5960

6061
const redirectUrlComplete =
6162
signInContext?.afterSignInUrl ?? signUpContext?.afterSignUpUrl ?? clerk?.buildAfterSignInUrl();
6263

64+
// If there are no pending tasks, navigate away from the tasks flow.
65+
// This handles cases where a user with an active session returns to the tasks URL,
66+
// for example by using browser back navigation. Since there are no pending tasks,
67+
// we redirect them to their intended destination.
6368
useEffect(() => {
64-
const task = clerk.session?.currentTask;
69+
if (isNavigatingToTask) {
70+
return;
71+
}
6572

66-
if (!task) {
73+
// Tasks can only exist on pending sessions, but we check both conditions
74+
// here to be defensive and ensure proper redirection
75+
const task = clerk.session?.currentTask;
76+
if (!task || clerk.session?.status === 'active') {
6777
void navigate(redirectUrlComplete);
6878
return;
6979
}
7080

7181
clerk.telemetry?.record(eventComponentMounted('SessionTask', { task: task.key }));
72-
}, [clerk, navigate, redirectUrlComplete]);
82+
}, [clerk, navigate, isNavigatingToTask, redirectUrlComplete]);
7383

7484
const nextTask = useCallback(() => {
75-
return clerk.__experimental_navigateToTask({ redirectUrlComplete });
85+
setIsNavigatingToTask(true);
86+
return clerk.__experimental_navigateToTask({ redirectUrlComplete }).finally(() => setIsNavigatingToTask(false));
7687
}, [clerk, redirectUrlComplete]);
7788

7889
if (!clerk.session?.currentTask) {

packages/clerk-js/src/ui/components/SessionTasks/tasks/ForceOrganizationSelection.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const ForceOrganizationSelectionFlows = () => {
2222
);
2323
}
2424

25+
// Do not render the organization selection flow when organization memberships
26+
// get invalidated after the create organization mutation
2527
if (hasData && currentFlow !== 'create-organization') {
2628
return <OrganizationSelectionPage setCurrentFlow={setCurrentFlow} />;
2729
}

0 commit comments

Comments
 (0)