Skip to content

Commit bba1cbe

Browse files
committed
Always throw error when rejecting prompt
1 parent 308e220 commit bba1cbe

File tree

6 files changed

+36
-168
lines changed

6 files changed

+36
-168
lines changed

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

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ import { allSettled, handleValueOrFn, noop } from '@clerk/shared/utils';
9999
import type { QueryClient } from '@tanstack/query-core';
100100

101101
import { debugLogger, initDebugLogger } from '@/utils/debug';
102-
import { CLERK_ENVIRONMENT_SETTING_PROMPT_REJECTED_KEY, SafeSessionStorage } from '@/utils/sessionStorage';
103102

104103
import type { MountComponentRenderer } from '../ui/Components';
105104
import {
@@ -744,32 +743,26 @@ export class Clerk implements ClerkInterface {
744743

745744
public __internal_attemptToEnableEnvironmentSetting = (
746745
params: __internal_AttemptToEnableEnvironmentSettingParams,
747-
): { status: 'enabled' | 'prompt-shown' | 'rejected' } => {
748-
const { for: setting, caller, onSuccess } = params;
746+
): { status: 'enabled' | 'prompt-shown' } => {
747+
const { for: setting, caller } = params;
749748

750749
// If not in development instance, return enabled status in order to not open the in-app prompt
751750
if (this.#instanceType !== 'development') {
752751
return { status: 'enabled' };
753752
}
754753

755-
const rejectedCallers = SafeSessionStorage.getItem<string[]>(CLERK_ENVIRONMENT_SETTING_PROMPT_REJECTED_KEY, []);
756-
757754
switch (setting) {
758755
case 'organizations':
759756
if (!disabledOrganizationsFeature(this, this.environment)) {
760757
return { status: 'enabled' };
761758
}
762759

763-
if (rejectedCallers.includes(caller)) {
764-
return { status: 'rejected' };
765-
}
766-
767760
this.__internal_openEnableOrganizationsPrompt({
768761
caller,
762+
// Reload current window to all invalidate all resources
763+
// related to organizations, eg: roles
769764
onSuccess: () => window.location.reload(),
770-
onClose: () => {
771-
SafeSessionStorage.setItem(CLERK_ENVIRONMENT_SETTING_PROMPT_REJECTED_KEY, [caller]);
772-
},
765+
onClose: params.onClose,
773766
} as __internal_EnableOrganizationsPromptProps);
774767

775768
return { status: 'prompt-shown' };
@@ -865,17 +858,13 @@ export class Clerk implements ClerkInterface {
865858
const { status } = this.__internal_attemptToEnableEnvironmentSetting({
866859
for: 'organizations',
867860
caller: 'OrganizationProfile',
868-
onSuccess: () => {
869-
this.openOrganizationProfile(props);
861+
onClose: () => {
862+
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationProfile'), {
863+
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
864+
});
870865
},
871866
});
872867

873-
if (status === 'rejected') {
874-
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationProfile'), {
875-
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
876-
});
877-
}
878-
879868
if (status === 'prompt-shown') {
880869
return;
881870
}
@@ -906,17 +895,13 @@ export class Clerk implements ClerkInterface {
906895
const { status } = this.__internal_attemptToEnableEnvironmentSetting({
907896
for: 'organizations',
908897
caller: 'CreateOrganization',
909-
onSuccess: () => {
910-
this.openCreateOrganization(props);
898+
onClose: () => {
899+
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('CreateOrganization'), {
900+
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
901+
});
911902
},
912903
});
913904

914-
if (status === 'rejected') {
915-
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('CreateOrganization'), {
916-
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
917-
});
918-
}
919-
920905
if (status === 'prompt-shown') {
921906
return;
922907
}
@@ -1059,17 +1044,13 @@ export class Clerk implements ClerkInterface {
10591044
const { status } = this.__internal_attemptToEnableEnvironmentSetting({
10601045
for: 'organizations',
10611046
caller: 'OrganizationProfile',
1062-
onSuccess: () => {
1063-
this.mountOrganizationProfile(node, props);
1047+
onClose: () => {
1048+
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationProfile'), {
1049+
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1050+
});
10641051
},
10651052
});
10661053

1067-
if (status === 'rejected') {
1068-
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationProfile'), {
1069-
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1070-
});
1071-
}
1072-
10731054
if (status === 'prompt-shown') {
10741055
return;
10751056
}
@@ -1110,17 +1091,13 @@ export class Clerk implements ClerkInterface {
11101091
const { status } = this.__internal_attemptToEnableEnvironmentSetting({
11111092
for: 'organizations',
11121093
caller: 'CreateOrganization',
1113-
onSuccess: () => {
1114-
this.mountCreateOrganization(node, props);
1094+
onClose: () => {
1095+
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('CreateOrganization'), {
1096+
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1097+
});
11151098
},
11161099
});
11171100

1118-
if (status === 'rejected') {
1119-
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('CreateOrganization'), {
1120-
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1121-
});
1122-
}
1123-
11241101
if (status === 'prompt-shown') {
11251102
return;
11261103
}
@@ -1152,17 +1129,13 @@ export class Clerk implements ClerkInterface {
11521129
const { status } = this.__internal_attemptToEnableEnvironmentSetting({
11531130
for: 'organizations',
11541131
caller: 'OrganizationSwitcher',
1155-
onSuccess: () => {
1156-
this.mountOrganizationSwitcher(node, props);
1132+
onClose: () => {
1133+
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationSwitcher'), {
1134+
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1135+
});
11571136
},
11581137
});
11591138

1160-
if (status === 'rejected') {
1161-
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationSwitcher'), {
1162-
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1163-
});
1164-
}
1165-
11661139
if (status === 'prompt-shown') {
11671140
return;
11681141
}
@@ -1202,17 +1175,13 @@ export class Clerk implements ClerkInterface {
12021175
const { status } = this.__internal_attemptToEnableEnvironmentSetting({
12031176
for: 'organizations',
12041177
caller: 'OrganizationList',
1205-
onSuccess: () => {
1206-
this.mountOrganizationList(node, props);
1178+
onClose: () => {
1179+
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationList'), {
1180+
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1181+
});
12071182
},
12081183
});
12091184

1210-
if (status === 'rejected') {
1211-
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('OrganizationList'), {
1212-
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1213-
});
1214-
}
1215-
12161185
if (status === 'prompt-shown') {
12171186
return;
12181187
}
@@ -1399,17 +1368,13 @@ export class Clerk implements ClerkInterface {
13991368
const { status } = this.__internal_attemptToEnableEnvironmentSetting({
14001369
for: 'organizations',
14011370
caller: 'TaskChooseOrganization',
1402-
onSuccess: () => {
1403-
this.mountTaskChooseOrganization(node, props);
1371+
onClose: () => {
1372+
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('TaskChooseOrganization'), {
1373+
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1374+
});
14041375
},
14051376
});
14061377

1407-
if (status === 'rejected') {
1408-
throw new ClerkRuntimeError(warnings.cannotRenderAnyOrganizationComponent('TaskChooseOrganization'), {
1409-
code: CANNOT_RENDER_ORGANIZATIONS_DISABLED_ERROR_CODE,
1410-
});
1411-
}
1412-
14131378
if (status === 'prompt-shown') {
14141379
return;
14151380
}

packages/clerk-js/src/core/resources/OrganizationSettings.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,4 @@ export class OrganizationSettings extends BaseResource implements OrganizationSe
7575
max_allowed_memberships: this.maxAllowedMemberships,
7676
} as unknown as OrganizationSettingsJSONSnapshot;
7777
}
78-
79-
/**
80-
* Used to enable the Organizations feature in memory after it has been enabled in the backend
81-
* from the devtools resource, since it cannot return the updated environment due to API caching
82-
*/
83-
public __internal_enableInMemory() {
84-
this.enabled = true;
85-
}
8678
}

packages/clerk-js/src/ui/components/devPrompts/EnableOrganizationsPrompt/index.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ const EnableOrganizationsPromptInternal = ({
1919
}: __internal_EnableOrganizationsPromptProps) => {
2020
const clerk = useClerk();
2121
const [isLoading, setIsLoading] = useState(false);
22-
23-
// @ts-expect-error - __unstable__environment is not typed
24-
const environment = clerk?.__unstable__environment as EnvironmentResource;
22+
const [isEnabled, setIsEnabled] = useState(false);
2523

2624
const handleEnableOrganizations = () => {
2725
setIsLoading(true);
@@ -31,18 +29,13 @@ const EnableOrganizationsPromptInternal = ({
3129
enable_organizations: true,
3230
})
3331
.then(() => {
34-
// The above mutation doesn't return an environment due to API caching, so we need to enable it in memory
35-
// as a persistent state
36-
// By the time the user refreshes the page, the environment will be updated and the prompt will not be shown again
37-
environment.organizationSettings.__internal_enableInMemory();
32+
setIsEnabled(true);
3833
})
3934
.finally(() => {
4035
setIsLoading(false);
4136
});
4237
};
4338

44-
const isEnabled = !!environment?.organizationSettings.enabled;
45-
4639
return (
4740
<Portal>
4841
<Modal

packages/clerk-js/src/utils/sessionStorage.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.

packages/shared/src/types/clerk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ export type __internal_AttemptToEnableEnvironmentSettingParams = {
14651465
| 'TaskChooseOrganization'
14661466
| 'useOrganizationList'
14671467
| 'useOrganization';
1468-
onSuccess: () => void;
1468+
onClose?: () => void;
14691469
};
14701470

14711471
type GoogleOneTapRedirectUrlProps = SignInForceRedirectUrl & SignUpForceRedirectUrl;

packages/vue/src/composables/useOrganization.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export const useOrganization: UseOrganization = () => {
6363
value.__internal_attemptToEnableEnvironmentSetting({
6464
for: 'organizations',
6565
caller: 'useOrganization',
66-
onSuccess: () => {},
6766
});
6867
unwatch();
6968
}

0 commit comments

Comments
 (0)