11import { useClerk } from '@clerk/shared/react' ;
22import { eventComponentMounted } from '@clerk/shared/telemetry' ;
3- import type { SessionTask } from '@clerk/types' ;
4- import { useCallback , useContext , useEffect } from 'react' ;
3+ import { useCallback , useContext , useEffect , useState } from 'react' ;
54
65import { Card } from '@/ui/elements/Card' ;
76import { withCardStateProvider } from '@/ui/elements/contexts' ;
87import { LoadingCardContainer } from '@/ui/elements/LoadingCard' ;
98
109import { SESSION_TASK_ROUTE_BY_KEY } from '../../../core/sessionTasks' ;
11- import { OrganizationListContext , SignInContext , SignUpContext } from '../../../ui/contexts' ;
12- import {
13- SessionTasksContext as SessionTasksContext ,
14- useSessionTasksContext ,
15- } from '../../contexts/components/SessionTasks' ;
10+ import { SignInContext , SignUpContext } from '../../../ui/contexts' ;
11+ import { SessionTasksContext , useSessionTasksContext } from '../../contexts/components/SessionTasks' ;
1612import { Route , Switch , useRouter } from '../../router' ;
17- import { OrganizationList } from '../OrganizationList ' ;
13+ import { ForceOrganizationSelectionTask } from './tasks/ForceOrganizationSelection ' ;
1814
19- const SessionTasksStart = withCardStateProvider ( ( ) => {
15+ const SessionTasksStart = ( ) => {
2016 const clerk = useClerk ( ) ;
2117 const { navigate } = useRouter ( ) ;
2218 const { redirectUrlComplete } = useSessionTasksContext ( ) ;
@@ -37,20 +33,13 @@ const SessionTasksStart = withCardStateProvider(() => {
3733 < Card . Footer />
3834 </ Card . Root >
3935 ) ;
40- } ) ;
36+ } ;
4137
4238function SessionTaskRoutes ( ) : JSX . Element {
4339 return (
4440 < Switch >
4541 < Route path = { SESSION_TASK_ROUTE_BY_KEY [ 'org' ] } >
46- < OrganizationListContext . Provider
47- value = { {
48- componentName : 'OrganizationList' ,
49- skipInvitationScreen : true ,
50- } }
51- >
52- < OrganizationList />
53- </ OrganizationListContext . Provider >
42+ < ForceOrganizationSelectionTask />
5443 </ Route >
5544 < Route index >
5645 < SessionTasksStart />
@@ -62,34 +51,55 @@ function SessionTaskRoutes(): JSX.Element {
6251/**
6352 * @internal
6453 */
65- export function SessionTask ( ) : JSX . Element {
54+ export const SessionTask = withCardStateProvider ( ( ) => {
6655 const clerk = useClerk ( ) ;
6756 const { navigate } = useRouter ( ) ;
6857 const signInContext = useContext ( SignInContext ) ;
6958 const signUpContext = useContext ( SignUpContext ) ;
59+ const [ isNavigatingToTask , setIsNavigatingToTask ] = useState ( false ) ;
7060
7161 const redirectUrlComplete =
7262 signInContext ?. afterSignInUrl ?? signUpContext ?. afterSignUpUrl ?? clerk ?. buildAfterSignInUrl ( ) ;
7363
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.
7468 useEffect ( ( ) => {
75- const task = clerk . session ?. currentTask ;
69+ if ( isNavigatingToTask ) {
70+ return ;
71+ }
7672
77- 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' ) {
7877 void navigate ( redirectUrlComplete ) ;
7978 return ;
8079 }
8180
8281 clerk . telemetry ?. record ( eventComponentMounted ( 'SessionTask' , { task : task . key } ) ) ;
83- } , [ clerk , navigate , redirectUrlComplete ] ) ;
82+ } , [ clerk , navigate , isNavigatingToTask , redirectUrlComplete ] ) ;
8483
85- const nextTask = useCallback (
86- ( ) => clerk . __experimental_navigateToTask ( { redirectUrlComplete } ) ,
87- [ clerk , redirectUrlComplete ] ,
88- ) ;
84+ const nextTask = useCallback ( ( ) => {
85+ setIsNavigatingToTask ( true ) ;
86+ return clerk . __experimental_navigateToTask ( { redirectUrlComplete } ) . finally ( ( ) => setIsNavigatingToTask ( false ) ) ;
87+ } , [ clerk , redirectUrlComplete ] ) ;
88+
89+ if ( ! clerk . session ?. currentTask ) {
90+ return (
91+ < Card . Root >
92+ < Card . Content >
93+ < LoadingCardContainer />
94+ </ Card . Content >
95+ < Card . Footer />
96+ </ Card . Root >
97+ ) ;
98+ }
8999
90100 return (
91101 < SessionTasksContext . Provider value = { { nextTask, redirectUrlComplete } } >
92102 < SessionTaskRoutes />
93103 </ SessionTasksContext . Provider >
94104 ) ;
95- }
105+ } ) ;
0 commit comments