Skip to content

Commit 036a853

Browse files
authored
fix(clerk-js): Do not switch after-auth screens on org invites revalidation (#6191)
1 parent 34d8c9f commit 036a853

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

.changeset/warm-parents-take.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Do not display create organization form after accepting organization invitation on after-auth flow

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useOrganizationList } from '@clerk/shared/react/index';
22
import type { PropsWithChildren } from 'react';
3-
import { useState } from 'react';
3+
import { useEffect, useRef, useState } from 'react';
44

55
import { OrganizationListContext } from '@/ui/contexts';
66
import { Card } from '@/ui/elements/Card';
@@ -16,6 +16,8 @@ import { organizationListParams } from '../../OrganizationSwitcher/utils';
1616
*/
1717
export const ForceOrganizationSelectionTask = withCardStateProvider(() => {
1818
const { userMemberships, userInvitations, userSuggestions } = useOrganizationList(organizationListParams);
19+
const currentFlow = useRef<'create-organization' | 'organization-selection'>();
20+
1921
const isLoading = userMemberships?.isLoading || userInvitations?.isLoading || userSuggestions?.isLoading;
2022
const hasData = !!(userMemberships?.count || userInvitations?.count || userSuggestions?.count);
2123

@@ -27,16 +29,27 @@ export const ForceOrganizationSelectionTask = withCardStateProvider(() => {
2729
);
2830
}
2931

30-
if (hasData) {
31-
return <OrganizationSelectionPage />;
32+
// Only show the organization selection page if organizations exist when the component first mounts.
33+
// This prevents unwanted screen transitions that could occur from data revalidation,
34+
// such as when a user accepts an organization invitation and the membership list updates.
35+
if (hasData || currentFlow.current === 'organization-selection') {
36+
return <OrganizationSelectionPage currentFlow={currentFlow} />;
3237
}
3338

34-
return <CreateOrganizationPage />;
39+
return <CreateOrganizationPage currentFlow={currentFlow} />;
3540
});
3641

37-
const OrganizationSelectionPage = () => {
42+
type CommonPageProps = {
43+
currentFlow: React.MutableRefObject<'create-organization' | 'organization-selection' | undefined>;
44+
};
45+
46+
const OrganizationSelectionPage = ({ currentFlow }: CommonPageProps) => {
3847
const [showCreateOrganizationForm, setShowCreateOrganizationForm] = useState(false);
3948

49+
useEffect(() => {
50+
currentFlow.current = 'organization-selection';
51+
}, [currentFlow]);
52+
4053
return (
4154
<OrganizationListContext.Provider
4255
value={{
@@ -66,7 +79,11 @@ const OrganizationSelectionPage = () => {
6679
);
6780
};
6881

69-
const CreateOrganizationPage = () => {
82+
const CreateOrganizationPage = ({ currentFlow }: CommonPageProps) => {
83+
useEffect(() => {
84+
currentFlow.current = 'create-organization';
85+
}, [currentFlow]);
86+
7087
return (
7188
<FlowCard>
7289
<Box

0 commit comments

Comments
 (0)