11import { getRedirectDisplay } from '@clerk/shared/internal/clerk-js/oauthConsent' ;
22import { useClerk , useOAuthConsent , useUser } from '@clerk/shared/react' ;
3- import { useState } from 'react' ;
3+ import { useEffect , useState } from 'react' ;
44
55import { useEnvironment , useOAuthConsentContext , withCoreUserGuard } from '@/ui/contexts' ;
66import { Box , Button , descriptors , Flow , Grid , localizationKeys , Text , useLocalizations } from '@/ui/customizables' ;
@@ -16,14 +16,7 @@ import { Route, Switch } from '@/ui/router';
1616import { getClientIdentityNotice } from './clientIdentity' ;
1717import { InlineAction } from './InlineAction' ;
1818import { 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' ;
2720import { LogoGroup , LogoGroupIcon , LogoGroupItem , LogoGroupItemContainer , LogoGroupSeparator } from './LogoGroup' ;
2821import { OrgSelect } from './OrgSelect' ;
2922import { 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