feat(studio): OAuth apps consent screen should allow to select another org (#53744)

## Problem

The OAuth apps consent screen does not allow users to change the
selected org. If the calling app didn't provide the org slug, we select
the first available org from the users orgs.

## Solution

Add an organization selector:
<img width="436" height="570" alt="image"
src="https://github.com/user-attachments/assets/a5579f2c-ff54-4d02-9d4a-ac75ed245719"
/>

## Review instructions

Requirements:
- have multiple orgs
- have a published app that needs projects selection and at least one
authorization already granted
- have the oauth example app ready

Tests:
- authorizing the app again on the same org it was before should
preselect the projects
- you should be able to select another org and if you do, that should
clear the selected projects and you should see only this org projects in
the options
- reselecting the initially granted org should reselect the projects it
had in the existing grant
This commit is contained in:
Gildas Garcia authored and GitHub committed 2026-11-20 18:16:48 +01:00
1 parent 1eb8684d60
commit e6986b5296
8 files changed
+130 -107

No files matched your search

@@ -6,16 +6,10 @@ import { OAuthAppsAuthorizeRequestData } from '@/data/oauth-apps/oauth-apps-auth
export interface AuthorizingAsCardProps {
email: string
request: OAuthAppsAuthorizeRequestData
organizationName: string
onSignOut: () => void
}
export const AuthorizingAsCard = ({
email,
request,
organizationName,
onSignOut,
}: AuthorizingAsCardProps) => {
export const AuthorizingAsCard = ({ email, request, onSignOut }: AuthorizingAsCardProps) => {
return (
<section className="flex flex-col gap-2">
<div className="divide-y rounded-md border bg-surface-75 px-4">
@@ -35,10 +29,6 @@ export const AuthorizingAsCard = ({
/>
</span>
</div>
<div className="flex items-center justify-between gap-4 py-2.5 text-xs">
<span className="shrink-0 text-foreground-light">Organization</span>
<span className="min-w-0 truncate text-right text-foreground">{organizationName}</span>
</div>
{!request.project_scoping_mode && (
<div className="flex items-center justify-between gap-4 py-2.5 text-xs">
<span className="shrink-0 text-foreground-light">Projects</span>
@@ -1,22 +1,18 @@
import { fireEvent, screen } from '@testing-library/react'
import { describe, expect, test, vi } from 'vitest'
import { screen } from '@testing-library/react'
import { describe, expect, test } from 'vitest'
import { NoProjectsNotice } from './NoProjectsNotice'
import { customRender } from '@/tests/lib/custom-render'
describe('NoProjectsNotice', () => {
test('interpolates the organization into the title', () => {
customRender(
<NoProjectsNotice appName="Vercel" organizationName="contoso-labs" onSwitchOrg={vi.fn()} />
)
customRender(<NoProjectsNotice appName="Vercel" organizationName="contoso-labs" />)
expect(screen.getByText('No projects in contoso-labs')).toBeInTheDocument()
})
test('interpolates the app name into the body', () => {
customRender(
<NoProjectsNotice appName="Vercel" organizationName="contoso-labs" onSwitchOrg={vi.fn()} />
)
customRender(<NoProjectsNotice appName="Vercel" organizationName="contoso-labs" />)
expect(
screen.getByText(
@@ -25,25 +21,8 @@ describe('NoProjectsNotice', () => {
).toBeInTheDocument()
})
test('fires onSwitchOrg when the inline link is clicked', () => {
const onSwitchOrg = vi.fn()
customRender(
<NoProjectsNotice
appName="Vercel"
organizationName="contoso-labs"
onSwitchOrg={onSwitchOrg}
/>
)
fireEvent.click(screen.getByText('Switch organization'))
expect(onSwitchOrg).toHaveBeenCalledTimes(1)
})
test('interpolates a different app name, so the copy is not hardcoded', () => {
customRender(
<NoProjectsNotice appName="Fabrikam" organizationName="contoso-labs" onSwitchOrg={vi.fn()} />
)
customRender(<NoProjectsNotice appName="Fabrikam" organizationName="contoso-labs" />)
expect(
screen.getByText(
@@ -53,13 +32,7 @@ describe('NoProjectsNotice', () => {
})
test('shows the same organization slug it was given', () => {
customRender(
<NoProjectsNotice
appName="Vercel"
organizationName="northwind-traders"
onSwitchOrg={vi.fn()}
/>
)
customRender(<NoProjectsNotice appName="Vercel" organizationName="northwind-traders" />)
expect(screen.getByText('No projects in northwind-traders')).toBeInTheDocument()
expect(screen.queryByText('No projects in contoso-labs')).not.toBeInTheDocument()
@@ -1,18 +1,11 @@
import { Admonition } from 'ui-patterns/Admonition'
import { InlineLinkClassName } from '@/components/ui/InlineLink'
export interface NoProjectsNoticeProps {
appName: string
organizationName: string
onSwitchOrg: () => void
}
export const NoProjectsNotice = ({
appName,
organizationName,
onSwitchOrg,
}: NoProjectsNoticeProps) => {
export const NoProjectsNotice = ({ appName, organizationName }: NoProjectsNoticeProps) => {
return (
<Admonition
type="default"
@@ -23,17 +16,6 @@ export const NoProjectsNotice = ({
{appName} needs access to at least one project, and this organization doesn't have any
yet.
</p>
<p>
Expecting to see your projects?
<button
type="button"
tabIndex={0}
className={InlineLinkClassName}
onClick={onSwitchOrg}
>
Switch organization
</button>
</p>
</>
}
/>
@@ -1,7 +1,7 @@
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { mockIntersectionObserver } from 'jsdom-testing-mocks'
import { HttpResponse } from 'msw'
import { describe, expect, test, vi } from 'vitest'
import { describe, expect, test } from 'vitest'
import { OAuthAppsAuthorizeScreen } from './OAuthAppsAuthorizeScreen'
import { OAuthAppsAuthorizeApproveData } from '@/data/oauth-apps/oauth-apps-authorize-approve-mutation'
@@ -28,7 +28,6 @@ function renderScreen({ organizationSlug, projectRef }: RenderScreenOptions = {}
authId="authId"
organizationSlug={organizationSlug}
projectRef={projectRef}
navigate={vi.fn()}
/>,
{
profileContext: createMockProfileContext(),
@@ -51,7 +50,7 @@ async function toggleProject(projectName: string) {
}
async function selectProject(projectName: string) {
fireEvent.click(await screen.findByRole('combobox'))
fireEvent.click(await screen.findByRole('combobox', { name: 'Projects' }))
await toggleProject(projectName)
}
@@ -370,7 +369,6 @@ describe('OAuthAppsAuthorizeScreen', () => {
})
renderScreen()
await screen.findByRole('combobox')
await selectProject('northwind-storefront')
fireEvent.click(await enabledAuthorizeButton(/Authorize Vercel/))
@@ -508,7 +506,7 @@ describe('OAuthAppsAuthorizeScreen', () => {
expect(await screen.findByText('All projects, including future ones')).toBeInTheDocument()
expect(await enabledAuthorizeButton()).toBeEnabled()
expect(screen.queryByRole('combobox')).not.toBeInTheDocument()
expect(screen.queryByRole('combobox', { name: 'Projects' })).not.toBeInTheDocument()
expect(
screen.queryByText('Must select at least one project to authorize.')
).not.toBeInTheDocument()
@@ -670,7 +668,7 @@ describe('OAuthAppsAuthorizeScreen', () => {
fireEvent.click(screen.getByRole('checkbox'))
expect(screen.queryByRole('combobox')).not.toBeInTheDocument()
expect(screen.queryByRole('combobox', { name: 'Projects' })).not.toBeInTheDocument()
const authorizeButton = screen.getByRole('button', { name: /Authorize Vercel/ })
expect(authorizeButton).toBeEnabled()
fireEvent.click(authorizeButton)
@@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { toast } from 'sonner'
import { Button } from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
@@ -9,6 +9,7 @@ import { AuthorizeSuccessScreen } from './AuthorizeSuccessScreen'
import { AuthorizingAsCard } from './AuthorizingAsCard'
import { NoProjectsNotice } from './NoProjectsNotice'
import { getFailedProjects, getPreselectedProjectRefs } from './OAuthAppsAuthorizeScreen.utils'
import { OrganizationSelect } from './OrganizationSelect'
import { MAX_SELECTED_PROJECTS, ProjectMultiSelect } from './ProjectMultiSelect'
import { ScopeGroupCard } from './ScopeGroupCard'
import { ScopeGroupCardWithDiff } from './ScopeGroupCardWithDiff'
@@ -38,14 +39,12 @@ export interface OAuthAppsAuthorizeScreenProps {
authId?: string
organizationSlug?: string
projectRef?: string
navigate: (destination: string) => void
}
export const OAuthAppsAuthorizeScreen = ({
authId,
organizationSlug,
organizationSlug: initialOrganizationSlug,
projectRef,
navigate,
}: OAuthAppsAuthorizeScreenProps) => {
const signOut = useSignOut()
@@ -75,6 +74,8 @@ export const OAuthAppsAuthorizeScreen = ({
isError: isErrorIdentity,
} = useProfile()
const [organizationSlug, setOrganizationSlug] = useState(initialOrganizationSlug)
const {
data: memberOrg,
error: memberOrgError,
@@ -85,8 +86,17 @@ export const OAuthAppsAuthorizeScreen = ({
projectRef,
request,
})
const [search, setSearch] = useState('')
const handleOrganizationChange = (slug: string) => {
if (slug === memberOrg?.slug) return
setOrganizationSlug(slug)
setSelectedProjectRefs([])
setAllProjectsSelected(false)
setSearch('')
}
const {
data: projectsData,
isPending: isPendingProjects,
@@ -171,17 +181,14 @@ export const OAuthAppsAuthorizeScreen = ({
}
)
const seeded = useRef(false)
useEffect(() => {
if (seeded.current || !preflightData || !memberOrg) return
if (!preflightData || !memberOrg) return
const grant = preflightData.existing_grant
const hasAllProjectsGrant =
grant !== null && (grant?.project_refs == null || grant?.project_refs?.length === 0)
if (!hasAllProjectsGrant && !initialProjectsData) return
seeded.current = true
if (hasAllProjectsGrant && isProjectScopingModeEnabled) {
setAllProjectsSelected(true)
return
@@ -380,13 +387,11 @@ export const OAuthAppsAuthorizeScreen = ({
window.location.reload()
}
const handleSwitchOrg = () => navigate('/organizations')
const handleDeselectFlagged = () =>
setSelectedProjectRefs((refs) => refs.filter((ref) => !flaggedRefs.includes(ref)))
const handleApprove = () => {
if (hasNoSelection) return
if (hasNoSelection || isBlockedOnProjects) return
const approveBody = usesSelectedProjects ? { project_refs: selectedProjectRefs } : {}
approveMutation.mutate({ auth_id: authId, slug: memberOrg.slug, body: approveBody })
}
@@ -462,19 +467,16 @@ export const OAuthAppsAuthorizeScreen = ({
<fieldset disabled={isSubmitting} className="contents">
<AuthorizingAsCard
email={profile!.primary_email}
organizationName={memberOrg.name}
onSignOut={handleSignOut}
request={request}
/>
<OrganizationSelect
organization={memberOrg}
onOrganizationChange={handleOrganizationChange}
/>
{isBlockedOnProjects && (
<NoProjectsNotice
appName={request.name}
organizationName={memberOrg.name}
onSwitchOrg={handleSwitchOrg}
/>
<NoProjectsNotice appName={request.name} organizationName={memberOrg.name} />
)}
{canProceed && isProjectScopingModeEnabled && (
<div className="flex flex-col gap-2">
<ProjectMultiSelect
@@ -532,21 +534,16 @@ export const OAuthAppsAuthorizeScreen = ({
</fieldset>
<div className="flex flex-col gap-2">
{canProceed ? (
<Button
block
variant={primaryActionVariant}
loading={isSubmitting || isPendingPreflight}
aria-label={primaryActionLabel}
onClick={hasRoleFailure ? handleDeselectFlagged : handleApprove}
>
{isSubmitting ? 'Authorizing...' : primaryActionLabel}
</Button>
) : (
<Button block variant="primary" onClick={handleSwitchOrg}>
Switch organization
</Button>
)}
<Button
block
variant={primaryActionVariant}
loading={isSubmitting || isPendingPreflight}
aria-label={primaryActionLabel}
onClick={hasRoleFailure ? handleDeselectFlagged : handleApprove}
>
{isSubmitting ? 'Authorizing...' : primaryActionLabel}
</Button>
{!isSubmitting && (
<Button variant="text" block onClick={handleDeny}>
Cancel
@@ -0,0 +1,86 @@
import { ChevronDownIcon } from 'lucide-react'
import { useState } from 'react'
import {
Button,
CheckIcon,
cn,
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
Label,
Popover,
PopoverContent,
PopoverTrigger,
} from 'ui'
import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
import { Organization } from '@/types'
export const OrganizationSelect = ({
organization: selectedOrganization,
onOrganizationChange,
}: {
organization: Organization
onOrganizationChange: (slug: string) => void
}) => {
const [open, setOpen] = useState(false)
const { data } = useOrganizationsQuery()
return (
<div className="flex flex-col gap-2">
<Label htmlFor="organization" className="text-foreground text-sm">
Organization
</Label>
<Popover open={open} onOpenChange={(open) => setOpen(open)}>
<PopoverTrigger asChild>
<Button
id="organization"
aria-expanded={open}
data-state={open ? 'open' : 'closed'}
type="button"
role="combobox"
className="justify-between"
size="small"
iconRight={<ChevronDownIcon />}
>
{selectedOrganization.name}
</Button>
</PopoverTrigger>
<PopoverContent align="end" className="w-(--radix-popover-trigger-width) p-0">
<Command>
<CommandInput placeholder="Search organization..." />
<CommandList>
<CommandEmpty>No organization found.</CommandEmpty>
<CommandGroup>
{(data ?? []).map((organization) => (
<CommandItem
key={organization.id}
value={organization.slug}
keywords={[organization.name]}
onSelect={() => {
onOrganizationChange(organization.slug)
setOpen(false)
}}
>
<CheckIcon
className={cn(
'mr-2 h-4 w-4',
selectedOrganization.slug === organization.slug
? 'opacity-100'
: 'opacity-0'
)}
/>
{organization.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
)
}
@@ -4,8 +4,6 @@ export { AuthorizingAsCard } from './AuthorizingAsCard'
export type { AuthorizingAsCardProps } from './AuthorizingAsCard'
export { OAuthAppsAuthorizeScreen } from './OAuthAppsAuthorizeScreen'
export type { OAuthAppsAuthorizeScreenProps } from './OAuthAppsAuthorizeScreen'
export { NoProjectsNotice } from './NoProjectsNotice'
export type { NoProjectsNoticeProps } from './NoProjectsNotice'
export { ProjectMultiSelect } from './ProjectMultiSelect'
export type { ProjectMultiSelectProps } from './ProjectMultiSelect'
export { ScopeGroupCard } from './ScopeGroupCard'
+1 -2
View File
@@ -26,11 +26,10 @@ function APIAuthorizationPage() {
<>
<title>{PAGE_TITLE}</title>
<OAuthAppsAuthorizeScreen
key={auth_id}
key={`${auth_id}_${organization_slug ?? ''}_${project_ref ?? ''}`}
authId={auth_id}
organizationSlug={organization_slug}
projectRef={organization_slug && project_ref ? project_ref : undefined}
navigate={(destination) => navigate({ href: destination })}
/>
</>
)