Skip to content

Commit e446bc9

Browse files
committed
Add guard for active session
1 parent d2074f6 commit e446bc9

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const warnings = {
2828
'The <SignUp/> component cannot render when a user is already signed in, unless the application allows multiple sessions. Since a user is signed in and this application only allows a single session, Clerk is redirecting to the value set in `afterSignUp` URL instead.',
2929
cannotRenderSignUpComponentWhenTaskExists:
3030
'The <SignUp/> component cannot render when a user has a pending task, unless the application allows multiple sessions. Since a user is signed in and this application only allows a single session, Clerk is redirecting to the task instead.',
31+
cannotRenderComponentWhenTaskDoesNotExist:
32+
'<TaskSelectOrganization/> cannot render unless a session task is pending. Clerk is redirecting to the value set in `redirectUrlComplete` instead.',
3133
cannotRenderSignInComponentWhenSessionExists:
3234
'The <SignIn/> component cannot render when a user is already signed in, unless the application allows multiple sessions. Since a user is signed in and this application only allows a single session, Clerk is redirecting to the `afterSignIn` URL instead.',
3335
cannotRenderSignInComponentWhenTaskExists:

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { useTaskSelectOrganizationContext } from '@/ui/contexts/components/Sessi
33
import { withCardStateProvider } from '@/ui/elements/contexts';
44

55
import { OrganizationList } from '../../OrganizationList';
6+
import { withTaskGuard } from './withTaskGuard';
67

7-
export const TaskSelectOrganization = withCardStateProvider(() => {
8+
const TaskSelectOrganizationInternal = () => {
89
const ctx = useTaskSelectOrganizationContext();
910

1011
return (
@@ -18,4 +19,6 @@ export const TaskSelectOrganization = withCardStateProvider(() => {
1819
<OrganizationList />
1920
</OrganizationListContext.Provider>
2021
);
21-
});
22+
};
23+
24+
export const TaskSelectOrganization = withTaskGuard(withCardStateProvider(TaskSelectOrganizationInternal));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { ComponentType } from 'react';
2+
3+
import { warnings } from '@/core/warnings';
4+
import { withRedirect } from '@/ui/common';
5+
import { useTaskSelectOrganizationContext } from '@/ui/contexts/components/SessionTasks';
6+
import type { AvailableComponentProps } from '@/ui/types';
7+
8+
export const withTaskGuard = <P extends AvailableComponentProps>(Component: ComponentType<P>) => {
9+
const displayName = Component.displayName || Component.name || 'Component';
10+
Component.displayName = displayName;
11+
12+
const HOC = (props: P) => {
13+
const ctx = useTaskSelectOrganizationContext();
14+
return withRedirect(
15+
Component,
16+
clerk => !!clerk.session && !clerk.session?.currentTask,
17+
({ clerk }) => ctx.redirectUrlComplete || clerk.buildAfterSignInUrl(),
18+
warnings.cannotRenderComponentWhenTaskDoesNotExist,
19+
)(props);
20+
};
21+
22+
HOC.displayName = `withTaskGuard(${displayName})`;
23+
24+
return HOC;
25+
};

0 commit comments

Comments
 (0)