Skip to content

Commit f6f67f9

Browse files
feat(clerk-js): Use org:sys_domains:read for improved fine grain control of the UI (clerk#1896)
* fix(clerk-js): Correct initial ref value for form feedback (clerk#1933) * chore(clerk-js): Use `org:sys_domains:read` * test(clerk-js): Use `org:sys_domains:read` * chore(clerk-js): Add changeset * chore(clerk-js): UseDomainList * feat(clerk-js): Session.isAuthorized now support the `any` parameter * chore(clerk-js): Improve relative url creation in DomainList * test(clerk-js): Test cases for trimLeadingSlash * chore(clerk-js): Address Pr comments --------- Co-authored-by: George Desipris <73396808+desiprisg@users.noreply.github.com>
1 parent 7b91aca commit f6f67f9

19 files changed

Lines changed: 337 additions & 204 deletions

‎.changeset/curly-news-push.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
'@clerk/types': patch
4+
---
5+
6+
Shows list of domains if member has the `org:sys_domain:read` permission.

‎packages/clerk-js/src/core/resources/Session.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class Session extends BaseResource implements SessionResource {
8282
* @experimental The method is experimental and subject to change in future releases.
8383
*/
8484
isAuthorized: IsAuthorized = async params => {
85-
return new Promise(resolve => {
85+
return new Promise((resolve, reject) => {
8686
// if there is no active organization user can not be authorized
8787
if (!this.lastActiveOrganizationId || !this.user) {
8888
return resolve(false);
@@ -106,7 +106,22 @@ export class Session extends BaseResource implements SessionResource {
106106
if (params.role) {
107107
return resolve(activeOrganizationRole === params.role);
108108
}
109-
return resolve(false);
109+
110+
if (params.any) {
111+
return resolve(
112+
!!params.any.find(permObj => {
113+
if (permObj.permission) {
114+
return activeOrganizationPermissions.includes(permObj.permission);
115+
}
116+
if (permObj.role) {
117+
return activeOrganizationRole === permObj.role;
118+
}
119+
return false;
120+
}),
121+
);
122+
}
123+
124+
return reject();
110125
});
111126
};
112127

‎packages/clerk-js/src/core/test/fixtures.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
export const mockJwt =
1414
'eyJhbGciOiJSUzI1NiIsImtpZCI6Imluc18yR0lvUWhiVXB5MGhYN0IyY1ZrdVRNaW5Yb0QiLCJ0eXAiOiJKV1QifQ.eyJhenAiOiJodHRwczovL2FjY291bnRzLmluc3BpcmVkLnB1bWEtNzQubGNsLmRldiIsImV4cCI6MTY2NjY0ODMxMCwiaWF0IjoxNjY2NjQ4MjUwLCJpc3MiOiJodHRwczovL2NsZXJrLmluc3BpcmVkLnB1bWEtNzQubGNsLmRldiIsIm5iZiI6MTY2NjY0ODI0MCwic2lkIjoic2Vzc18yR2JEQjRlbk5kQ2E1dlMxenBDM1h6Zzl0SzkiLCJzdWIiOiJ1c2VyXzJHSXBYT0VwVnlKdzUxcmtabjlLbW5jNlN4ciJ9.n1Usc-DLDftqA0Xb-_2w8IGs4yjCmwc5RngwbSRvwevuZOIuRoeHmE2sgCdEvjfJEa7ewL6EVGVcM557TWPW--g_J1XQPwBy8tXfz7-S73CEuyRFiR97L2AHRdvRtvGtwR-o6l8aHaFxtlmfWbQXfg4kFJz2UGe9afmh3U9-f_4JOZ5fa3mI98UMy1-bo20vjXeWQ9aGrqaxHQxjnzzC-1Kpi5LdPvhQ16H0dPB8MHRTSM5TAuLKTpPV7wqixmbtcc2-0k6b9FKYZNqRVTaIyV-lifZloBvdzlfOF8nW1VVH_fx-iW5Q3hovHFcJIULHEC1kcAYTubbxzpgeVQepGg';
1515

16-
type OrgParams = Partial<OrganizationJSON> & { role?: MembershipRole; permissions?: OrganizationPermission[] };
16+
export type OrgParams = Partial<OrganizationJSON> & { role?: MembershipRole; permissions?: OrganizationPermission[] };
1717

1818
type WithUserParams = Omit<
1919
Partial<UserJSON>,

‎packages/clerk-js/src/ui/common/Gate.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { IsAuthorized, OrganizationPermission } from '@clerk/types';
1+
import type { IsAuthorized } from '@clerk/types';
22
import type { ComponentType, PropsWithChildren, ReactNode } from 'react';
33
import React, { useEffect } from 'react';
44

55
import { useCoreSession } from '../contexts';
66
import { useFetch } from '../hooks';
77
import { useRouter } from '../router';
88

9-
type GateParams = Omit<Parameters<IsAuthorized>[0], 'permission'> & { permission: OrganizationPermission };
9+
type GateParams = Parameters<IsAuthorized>[0];
1010
type GateProps = PropsWithChildren<
1111
GateParams & {
1212
fallback?: ReactNode;

‎packages/clerk-js/src/ui/components/OrganizationProfile/DomainList.tsx‎

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { GetDomainsParams, OrganizationEnrollmentMode } from '@clerk/types';
1+
import type { GetDomainsParams, OrganizationDomainResource, OrganizationEnrollmentMode } from '@clerk/types';
22
import type { OrganizationDomainVerificationStatus } from '@clerk/types';
33
import React, { useMemo } from 'react';
44

5-
import { withGate } from '../../common';
5+
import { stripOrigin, toURL, trimLeadingSlash } from '../../../utils';
6+
import { useGate, withGate } from '../../common';
67
import { useCoreOrganization } from '../../contexts';
8+
import type { LocalizationKey } from '../../customizables';
79
import { Box, Col, localizationKeys, Spinner } from '../../customizables';
810
import { ArrowBlockButton, BlockWithTrailingComponent, ThreeDotsMenu } from '../../elements';
911
import { useInView } from '../../hooks';
@@ -17,10 +19,61 @@ type DomainListProps = GetDomainsParams & {
1719
* Enables internal links to navigate to the correct page
1820
* based on when this component is used
1921
*/
20-
redirectSubPath: string;
22+
redirectSubPath: 'organization-settings/domain' | 'domain';
2123
fallback?: React.ReactNode;
2224
};
2325

26+
const useDomainList = () => {
27+
const { isAuthorizedUser: canDeleteDomain } = useGate({ permission: 'org:sys_domains:delete' });
28+
const { isAuthorizedUser: canVerifyDomain } = useGate({ permission: 'org:sys_domains:manage' });
29+
30+
return {
31+
showDotMenu: canDeleteDomain || canVerifyDomain,
32+
canVerifyDomain,
33+
canDeleteDomain,
34+
};
35+
};
36+
37+
const buildDomainListRelativeURL = (parentPath: string, domainId: string, mode?: 'verify' | 'remove') =>
38+
trimLeadingSlash(stripOrigin(toURL(`${parentPath}/${domainId}/${mode || ''}`)));
39+
40+
const useMenuActions = (
41+
parentPath: string,
42+
domainId: string,
43+
): { label: LocalizationKey; onClick: () => Promise<unknown>; isDestructive?: boolean }[] => {
44+
const { canDeleteDomain, canVerifyDomain } = useDomainList();
45+
const { navigate } = useRouter();
46+
47+
const menuActions = [];
48+
49+
if (canVerifyDomain) {
50+
menuActions.push({
51+
label: localizationKeys('organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__verify'),
52+
onClick: () => navigate(buildDomainListRelativeURL(parentPath, domainId, 'verify')),
53+
});
54+
}
55+
56+
if (canDeleteDomain) {
57+
menuActions.push({
58+
label: localizationKeys('organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__remove'),
59+
isDestructive: true,
60+
onClick: () => navigate(buildDomainListRelativeURL(parentPath, domainId, 'remove')),
61+
});
62+
}
63+
64+
return menuActions;
65+
};
66+
67+
const DomainListDotMenu = ({
68+
redirectSubPath,
69+
domainId,
70+
}: Pick<DomainListProps, 'redirectSubPath'> & {
71+
domainId: OrganizationDomainResource['id'];
72+
}) => {
73+
const actions = useMenuActions(redirectSubPath, domainId);
74+
return <ThreeDotsMenu actions={actions} />;
75+
};
76+
2477
export const DomainList = withGate(
2578
(props: DomainListProps) => {
2679
const { verificationStatus, enrollmentMode, redirectSubPath, fallback, ...rest } = props;
@@ -31,6 +84,7 @@ export const DomainList = withGate(
3184
},
3285
});
3386

87+
const { showDotMenu } = useDomainList();
3488
const { ref } = useInView({
3589
threshold: 0,
3690
onChange: inView => {
@@ -69,7 +123,7 @@ export const DomainList = withGate(
69123
<Col>
70124
{domainList.length === 0 && !domains?.isLoading && fallback}
71125
{domainList.map(d => {
72-
if (!(d.verification && d.verification.status === 'verified')) {
126+
if (!(d.verification && d.verification.status === 'verified') || !showDotMenu) {
73127
return (
74128
<BlockWithTrailingComponent
75129
key={d.id}
@@ -82,23 +136,12 @@ export const DomainList = withGate(
82136
})}
83137
badge={<EnrollmentBadge organizationDomain={d} />}
84138
trailingComponent={
85-
<ThreeDotsMenu
86-
actions={[
87-
{
88-
label: localizationKeys(
89-
'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__verify',
90-
),
91-
onClick: () => navigate(`${redirectSubPath}${d.id}/verify`),
92-
},
93-
{
94-
label: localizationKeys(
95-
'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__remove',
96-
),
97-
isDestructive: true,
98-
onClick: () => navigate(`${redirectSubPath}${d.id}/remove`),
99-
},
100-
]}
101-
/>
139+
showDotMenu ? (
140+
<DomainListDotMenu
141+
redirectSubPath={redirectSubPath}
142+
domainId={d.id}
143+
/>
144+
) : undefined
102145
}
103146
>
104147
{d.name}
@@ -116,7 +159,7 @@ export const DomainList = withGate(
116159
padding: `${t.space.$3} ${t.space.$4}`,
117160
minHeight: t.sizes.$10,
118161
})}
119-
onClick={() => navigate(`${redirectSubPath}${d.id}`)}
162+
onClick={() => navigate(buildDomainListRelativeURL(redirectSubPath, d.id))}
120163
>
121164
{d.name}
122165
</ArrowBlockButton>
@@ -154,6 +197,6 @@ export const DomainList = withGate(
154197
);
155198
},
156199
{
157-
permission: 'org:sys_domains:manage',
200+
permission: 'org:sys_domains:read',
158201
},
159202
);

‎packages/clerk-js/src/ui/components/OrganizationProfile/OrganizationMembersTabInvitations.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const OrganizationMembersTabInvitations = () => {
5858
onClick={() => navigate('organization-settings/domain')}
5959
/>
6060
}
61-
redirectSubPath={'organization-settings/domain/'}
61+
redirectSubPath={'organization-settings/domain'}
6262
verificationStatus={'verified'}
6363
enrollmentMode={'automatic_invitation'}
6464
/>

‎packages/clerk-js/src/ui/components/OrganizationProfile/OrganizationMembersTabRequests.tsx‎

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BlockButton } from '../../common';
2-
import { useOrganizationProfileContext } from '../../contexts';
1+
import { BlockButton, Gate } from '../../common';
2+
import { useEnvironment, useOrganizationProfileContext } from '../../contexts';
33
import { Col, Flex, localizationKeys } from '../../customizables';
44
import { Header } from '../../elements';
55
import { useRouter } from '../../router';
@@ -8,10 +8,13 @@ import { MembershipWidget } from './MembershipWidget';
88
import { RequestToJoinList } from './RequestToJoinList';
99

1010
export const OrganizationMembersTabRequests = () => {
11+
const { organizationSettings } = useEnvironment();
1112
const { navigate } = useRouter();
1213
//@ts-expect-error
1314
const { __unstable_manageBillingUrl } = useOrganizationProfileContext();
1415

16+
const isDomainsEnabled = organizationSettings?.domains?.enabled;
17+
1518
return (
1619
<Col
1720
gap={8}
@@ -20,42 +23,47 @@ export const OrganizationMembersTabRequests = () => {
2023
}}
2124
>
2225
{__unstable_manageBillingUrl && <MembershipWidget />}
23-
<Col
24-
gap={2}
25-
sx={{
26-
width: '100%',
27-
}}
28-
>
29-
<Header.Root>
30-
<Header.Title
31-
localizationKey={localizationKeys(
32-
'organizationProfile.membersPage.requestsTab.autoSuggestions.headerTitle',
33-
)}
34-
textVariant='largeMedium'
35-
/>
36-
<Header.Subtitle
37-
localizationKey={localizationKeys(
38-
'organizationProfile.membersPage.requestsTab.autoSuggestions.headerSubtitle',
39-
)}
40-
variant='regularRegular'
41-
/>
42-
</Header.Root>
43-
<DomainList
44-
fallback={
45-
<BlockButton
46-
colorScheme='primary'
47-
textLocalizationKey={localizationKeys(
48-
'organizationProfile.membersPage.requestsTab.autoSuggestions.primaryButton',
49-
)}
50-
id='manageVerifiedDomains'
51-
onClick={() => navigate('organization-settings/domain')}
26+
27+
{isDomainsEnabled && (
28+
<Gate permission='org:sys_domains:manage'>
29+
<Col
30+
gap={2}
31+
sx={{
32+
width: '100%',
33+
}}
34+
>
35+
<Header.Root>
36+
<Header.Title
37+
localizationKey={localizationKeys(
38+
'organizationProfile.membersPage.requestsTab.autoSuggestions.headerTitle',
39+
)}
40+
textVariant='largeMedium'
41+
/>
42+
<Header.Subtitle
43+
localizationKey={localizationKeys(
44+
'organizationProfile.membersPage.requestsTab.autoSuggestions.headerSubtitle',
45+
)}
46+
variant='regularRegular'
47+
/>
48+
</Header.Root>
49+
<DomainList
50+
fallback={
51+
<BlockButton
52+
colorScheme='primary'
53+
textLocalizationKey={localizationKeys(
54+
'organizationProfile.membersPage.requestsTab.autoSuggestions.primaryButton',
55+
)}
56+
id='manageVerifiedDomains'
57+
onClick={() => navigate('organization-settings/domain')}
58+
/>
59+
}
60+
redirectSubPath={'organization-settings/domain'}
61+
verificationStatus={'verified'}
62+
enrollmentMode={'automatic_suggestion'}
5263
/>
53-
}
54-
redirectSubPath={'organization-settings/domain/'}
55-
verificationStatus={'verified'}
56-
enrollmentMode={'automatic_suggestion'}
57-
/>
58-
</Col>
64+
</Col>
65+
</Gate>
66+
)}
5967

6068
<Flex
6169
direction='col'

‎packages/clerk-js/src/ui/components/OrganizationProfile/OrganizationProfileRoutes.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const OrganizationProfileRoutes = (props: PropsOfComponent<typeof Profile
7777
</Route>
7878
<Route path=':id'>
7979
<Gate
80-
permission={'org:sys_domains:manage'}
80+
any={[{ permission: 'org:sys_domains:manage' }, { permission: 'org:sys_domains:delete' }]}
8181
redirectTo='../../'
8282
>
8383
<VerifiedDomainPage />

‎packages/clerk-js/src/ui/components/OrganizationProfile/OrganizationSettings.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const OrganizationSettings = () => {
2626
<Header.Subtitle localizationKey={localizationKeys('organizationProfile.start.headerSubtitle__settings')} />
2727
</Header.Root>
2828
<OrganizationProfileSection />
29-
<Gate permission='org:sys_domains:manage'>
29+
<Gate permission='org:sys_domains:read'>
3030
<OrganizationDomainsSection />
3131
</Gate>
3232
<OrganizationDangerSection />
@@ -85,7 +85,7 @@ const OrganizationDomainsSection = () => {
8585
subtitle={localizationKeys('organizationProfile.profilePage.domainSection.subtitle')}
8686
id='organizationDomains'
8787
>
88-
<DomainList redirectSubPath={'domain/'} />
88+
<DomainList redirectSubPath={'domain'} />
8989

9090
<AddBlockButton
9191
textLocalizationKey={localizationKeys('organizationProfile.profilePage.domainSection.primaryButton')}

0 commit comments

Comments
 (0)