|
1 | 1 | import { useClerk } from '@clerk/shared/react';
|
2 | 2 | import { eventComponentMounted } from '@clerk/shared/telemetry';
|
3 |
| -import { useCallback, useContext, useEffect } from 'react'; |
| 3 | +import { useCallback, useContext, useEffect, useState } from 'react'; |
4 | 4 |
|
5 | 5 | import { Card } from '@/ui/elements/Card';
|
6 | 6 | import { withCardStateProvider } from '@/ui/elements/contexts';
|
@@ -56,23 +56,34 @@ export const SessionTask = withCardStateProvider(() => {
|
56 | 56 | const { navigate } = useRouter();
|
57 | 57 | const signInContext = useContext(SignInContext);
|
58 | 58 | const signUpContext = useContext(SignUpContext);
|
| 59 | + const [isNavigatingToTask, setIsNavigatingToTask] = useState(false); |
59 | 60 |
|
60 | 61 | const redirectUrlComplete =
|
61 | 62 | signInContext?.afterSignInUrl ?? signUpContext?.afterSignUpUrl ?? clerk?.buildAfterSignInUrl();
|
62 | 63 |
|
| 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. |
63 | 68 | useEffect(() => {
|
64 |
| - const task = clerk.session?.currentTask; |
| 69 | + if (isNavigatingToTask) { |
| 70 | + return; |
| 71 | + } |
65 | 72 |
|
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') { |
67 | 77 | void navigate(redirectUrlComplete);
|
68 | 78 | return;
|
69 | 79 | }
|
70 | 80 |
|
71 | 81 | clerk.telemetry?.record(eventComponentMounted('SessionTask', { task: task.key }));
|
72 |
| - }, [clerk, navigate, redirectUrlComplete]); |
| 82 | + }, [clerk, navigate, isNavigatingToTask, redirectUrlComplete]); |
73 | 83 |
|
74 | 84 | const nextTask = useCallback(() => {
|
75 |
| - return clerk.__experimental_navigateToTask({ redirectUrlComplete }); |
| 85 | + setIsNavigatingToTask(true); |
| 86 | + return clerk.__experimental_navigateToTask({ redirectUrlComplete }).finally(() => setIsNavigatingToTask(false)); |
76 | 87 | }, [clerk, redirectUrlComplete]);
|
77 | 88 |
|
78 | 89 | if (!clerk.session?.currentTask) {
|
|
0 commit comments