Skip to content

Commit 30446c5

Browse files
feat(ui): make OAuth consent scopes selectable (#10134)
1 parent de975fa commit 30446c5

8 files changed

Lines changed: 309 additions & 39 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@clerk/ui': minor
3+
---
4+
5+
Allow users to select or clear optional scopes in `<OAuthConsent />`. Only selected scopes are submitted. When shown, `offline_access` remains checked and cannot be cleared.
6+
7+
Customize the scope checkboxes with the new `listGroupItemCheckbox` appearance element. Checkboxes align with the first line of descriptions that wrap onto multiple lines.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/ui': patch
3+
---
4+
5+
Fix a stray scrollbar on the `<OAuthConsent />` and `<OAuthDeviceVerification />` scope list when only one scope is shown.

‎packages/clerk-js/sandbox/app.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,19 @@ void (async () => {
466466
},
467467
'/oauth-consent': () => {
468468
const searchParams = new URLSearchParams(window.location.search);
469-
const scopes = (searchParams.get('scope')?.split(',') ?? []).map(scope => ({
470-
scope,
471-
description: scope === 'offline_access' ? null : `Grants access to your ${scope}`,
472-
requires_consent: true,
473-
}));
469+
const scopes = [
470+
...(searchParams.get('scope')?.split(',') ?? []).map(scope => ({
471+
scope,
472+
description: scope === 'offline_access' ? null : `Grants access to your ${scope}`,
473+
requires_consent: true,
474+
})),
475+
{
476+
scope: 'sandbox:long_description',
477+
description:
478+
'Grants access to read and update your profile, connected accounts, and organization memberships across every workspace you belong to',
479+
requires_consent: true,
480+
},
481+
];
474482
Clerk.mountOAuthConsent(
475483
app,
476484
componentControls.oauthConsent.getProps() ?? {

‎packages/ui/src/components/OAuthConsent/ListGroup.tsx‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ComponentProps } from 'react';
22

3-
import { Box, descriptors, Text } from '@/ui/customizables';
3+
import { Box, CheckboxInput, descriptors, Flex, Text } from '@/ui/customizables';
4+
import type { InternalTheme } from '@/ui/styledSystem';
45
import { common } from '@/ui/styledSystem';
56
import { colors } from '@/ui/utils/colors';
67

@@ -74,7 +75,7 @@ export function ListGroupContent({
7475
backgroundColor: t.colors.$colorBackground,
7576
overflowY: 'auto',
7677
...common.unstyledScrollbar(t),
77-
'--fade-distance': '2.5rem',
78+
'--fade-distance': '2rem',
7879
'&::before, &::after': {
7980
content: '""',
8081
display: 'block',
@@ -131,6 +132,14 @@ export function ListGroupContent({
131132
);
132133
}
133134

135+
const listGroupItemStyles = (t: InternalTheme) => ({
136+
paddingInline: t.space.$3,
137+
paddingBlock: t.space.$2,
138+
borderTopWidth: t.borderWidths.$normal,
139+
borderTopStyle: t.borderStyles.$solid,
140+
borderTopColor: t.colors.$borderAlpha100,
141+
});
142+
134143
export function ListGroupItem({
135144
children,
136145
sx,
@@ -142,13 +151,9 @@ export function ListGroupItem({
142151
as='li'
143152
sx={[
144153
t => ({
154+
...listGroupItemStyles(t),
145155
display: 'flex',
146156
alignItems: 'baseline',
147-
paddingInline: t.space.$3,
148-
paddingBlock: t.space.$2,
149-
borderTopWidth: t.borderWidths.$normal,
150-
borderTopStyle: t.borderStyles.$solid,
151-
borderTopColor: t.colors.$borderAlpha100,
152157
'&::before': {
153158
content: '""',
154159
display: 'inline-block',
@@ -179,3 +184,36 @@ export function ListGroupItemLabel(props: Omit<ComponentProps<typeof Text>, 'ele
179184
/>
180185
);
181186
}
187+
188+
export function ListGroupCheckboxItem({
189+
children,
190+
...props
191+
}: Pick<ComponentProps<typeof CheckboxInput>, 'checked' | 'value' | 'isDisabled' | 'onChange'> & {
192+
children: ComponentProps<typeof Text>['children'];
193+
}) {
194+
return (
195+
<Box
196+
as='li'
197+
sx={listGroupItemStyles}
198+
elementDescriptor={descriptors.listGroupItem}
199+
>
200+
<Flex
201+
as='label'
202+
align='start'
203+
gap={2}
204+
sx={{ cursor: 'pointer', userSelect: 'none' }}
205+
>
206+
<CheckboxInput
207+
{...props}
208+
elementDescriptor={descriptors.listGroupItemCheckbox}
209+
sx={t => ({
210+
flexShrink: 0,
211+
cursor: 'pointer',
212+
marginBlockStart: `calc((${t.fontSizes.$md} * ${t.lineHeights.$small} - ${t.sizes.$3x5}) / 2)`,
213+
})}
214+
/>
215+
<ListGroupItemLabel as='span'>{children}</ListGroupItemLabel>
216+
</Flex>
217+
</Box>
218+
);
219+
}

‎packages/ui/src/components/OAuthConsent/OAuthConsent.tsx‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getRedirectDisplay } from '@clerk/shared/internal/clerk-js/oauthConsent';
22
import { useClerk, useOAuthConsent, useUser } from '@clerk/shared/react';
3-
import { useState } from 'react';
3+
import { useEffect, useState } from 'react';
44

55
import { useEnvironment, useOAuthConsentContext, withCoreUserGuard } from '@/ui/contexts';
66
import { Box, Button, descriptors, Flow, Grid, localizationKeys, Text, useLocalizations } from '@/ui/customizables';
@@ -16,14 +16,7 @@ import { Route, Switch } from '@/ui/router';
1616
import { getClientIdentityNotice } from './clientIdentity';
1717
import { InlineAction } from './InlineAction';
1818
import { getKnownOAuthClient } from './knownClients';
19-
import {
20-
ListGroup,
21-
ListGroupContent,
22-
ListGroupHeader,
23-
ListGroupHeaderTitle,
24-
ListGroupItem,
25-
ListGroupItemLabel,
26-
} from './ListGroup';
19+
import { ListGroup, ListGroupCheckboxItem, ListGroupContent, ListGroupHeader, ListGroupHeaderTitle } from './ListGroup';
2720
import { LogoGroup, LogoGroupIcon, LogoGroupItem, LogoGroupItemContainer, LogoGroupSeparator } from './LogoGroup';
2821
import { OrgSelect } from './OrgSelect';
2922
import { getForwardedParams, getOAuthConsentFromSearch, getRedirectUriFromSearch } from './utils';
@@ -43,6 +36,7 @@ function _OAuthConsent() {
4336
const [isUriModalOpen, setIsUriModalOpen] = useState(false);
4437

4538
const [selectedOrg, setSelectedOrg] = useState<string | null>(null);
39+
const [uncheckedScopes, setUncheckedScopes] = useState<string[]>([]);
4640

4741
// onAllow and onDeny are always provided as a pair by the accounts portal.
4842
const hasContextCallbacks = Boolean(ctx.onAllow || ctx.onDeny);
@@ -52,6 +46,10 @@ function _OAuthConsent() {
5246
const oauthClientId = ctx.oauthClientId ?? fromUrl.oauthClientId;
5347
const scope = ctx.scope ?? fromUrl.scope;
5448

49+
useEffect(() => {
50+
setUncheckedScopes([]);
51+
}, [oauthClientId, scope]);
52+
5553
// Public path: fetch via hook. Disabled on the accounts portal path
5654
// (which already has all data via context) to avoid a wasted FAPI request.
5755
const redirectUri = ctx.redirectUrl ?? getRedirectUriFromSearch();
@@ -72,12 +70,14 @@ function _OAuthConsent() {
7270

7371
// Context (accounts portal path) wins over hook data (public path).
7472
const scopes = ctx.scopes ?? mappedHookScopes ?? [];
73+
const selectedScopes = scopes.filter(item => !uncheckedScopes.includes(item.scope));
74+
const selectedScope = selectedScopes.map(item => item.scope).join(' ') || OFFLINE_ACCESS_SCOPE;
7575
const oauthApplicationName = ctx.oauthApplicationName ?? data?.oauthApplicationName ?? '';
7676
const oauthApplicationLogoUrl = ctx.oauthApplicationLogoUrl ?? data?.oauthApplicationLogoUrl;
7777
const oauthApplicationUrl = ctx.oauthApplicationUrl ?? data?.oauthApplicationUrl;
7878
const redirectUrl = ctx.redirectUrl ?? redirectUri;
7979

80-
const hasOrgReadScope = scopes.some(s => s.scope === USER_ORG_READ_SCOPE);
80+
const hasOrgReadScope = selectedScopes.some(s => s.scope === USER_ORG_READ_SCOPE);
8181
const orgSelectionEnabled = !!(hasOrgReadScope && organizationSettings.enabled);
8282
const orgOptions = orgSelectionEnabled
8383
? (user?.organizationMemberships ?? []).map(m => ({
@@ -134,7 +134,9 @@ function _OAuthConsent() {
134134
}
135135

136136
const actionUrl = clerk.oauthApplication.buildConsentActionUrl({ clientId: oauthClientId });
137-
const forwardedParams = getForwardedParams();
137+
const forwardedParams = getForwardedParams().filter(
138+
([key]) => !['scope', 'organization_id', 'consented'].includes(key),
139+
);
138140

139141
// Accounts portal path delegates to context callbacks; public path lets the form submit natively.
140142
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
@@ -152,15 +154,13 @@ function _OAuthConsent() {
152154

153155
const primaryIdentifier = user?.primaryEmailAddress?.emailAddress || user?.primaryPhoneNumber?.phoneNumber;
154156

155-
const displayedScopes = scopes
156-
.filter(item => item.scope !== OFFLINE_ACCESS_SCOPE)
157-
.map(item => ({
158-
...item,
159-
description:
160-
item.scope === PRIVATE_METADATA_SCOPE
161-
? t(localizationKeys('oauthConsent.scopeList.privateMetadata', { applicationName }))
162-
: item.description,
163-
}));
157+
const displayedScopes = scopes.map(item => ({
158+
...item,
159+
description:
160+
item.scope === PRIVATE_METADATA_SCOPE
161+
? t(localizationKeys('oauthConsent.scopeList.privateMetadata', { applicationName }))
162+
: item.description,
163+
}));
164164
const hasOfflineAccess = scopes.some(item => item.scope === OFFLINE_ACCESS_SCOPE);
165165

166166
return (
@@ -285,9 +285,20 @@ function _OAuthConsent() {
285285
</ListGroupHeader>
286286
<ListGroupContent>
287287
{displayedScopes.map(item => (
288-
<ListGroupItem key={item.scope}>
289-
<ListGroupItemLabel>{item.description || item.scope || ''}</ListGroupItemLabel>
290-
</ListGroupItem>
288+
<ListGroupCheckboxItem
289+
key={item.scope}
290+
value={item.scope}
291+
checked={!uncheckedScopes.includes(item.scope)}
292+
isDisabled={hasContextCallbacks || item.scope === OFFLINE_ACCESS_SCOPE}
293+
onChange={e => {
294+
const checked = e.target.checked;
295+
setUncheckedScopes(previous =>
296+
checked ? previous.filter(scope => scope !== item.scope) : [...previous, item.scope],
297+
);
298+
}}
299+
>
300+
{item.description || item.scope || ''}
301+
</ListGroupCheckboxItem>
291302
))}
292303
</ListGroupContent>
293304
</ListGroup>
@@ -354,6 +365,13 @@ function _OAuthConsent() {
354365
value={value}
355366
/>
356367
))}
368+
{!hasContextCallbacks && (
369+
<input
370+
type='hidden'
371+
name='scope'
372+
value={selectedScope}
373+
/>
374+
)}
357375
{!hasContextCallbacks && orgSelectionEnabled && effectiveOrg && (
358376
<input
359377
type='hidden'

0 commit comments

Comments
 (0)