From e6986b52965626b78c6afdd375fd22c861663e16 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:16:48 +0100 Subject: [PATCH] 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: image ## 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 --- .../OAuthApps/Consent/AuthorizingAsCard.tsx | 12 +-- .../Consent/NoProjectsNotice.test.tsx | 39 ++------- .../OAuthApps/Consent/NoProjectsNotice.tsx | 20 +---- .../Consent/OAuthAppsAuthorizeScreen.test.tsx | 10 +-- .../Consent/OAuthAppsAuthorizeScreen.tsx | 65 +++++++------- .../OAuthApps/Consent/OrganizationSelect.tsx | 86 +++++++++++++++++++ .../Organization/OAuthApps/Consent/index.ts | 2 - apps/studio/routes/authorize.tsx | 3 +- 8 files changed, 130 insertions(+), 107 deletions(-) create mode 100644 apps/studio/components/interfaces/Organization/OAuthApps/Consent/OrganizationSelect.tsx diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/AuthorizingAsCard.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/AuthorizingAsCard.tsx index f5b0e9f7497..00b4fba04ba 100644 --- a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/AuthorizingAsCard.tsx +++ b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/AuthorizingAsCard.tsx @@ -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 (
@@ -35,10 +29,6 @@ export const AuthorizingAsCard = ({ />
-
- Organization - {organizationName} -
{!request.project_scoping_mode && (
Projects diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.test.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.test.tsx index f2210a5fe09..e82fa8a1d44 100644 --- a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.test.tsx +++ b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.test.tsx @@ -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( - - ) + customRender() expect(screen.getByText('No projects in contoso-labs')).toBeInTheDocument() }) test('interpolates the app name into the body', () => { - customRender( - - ) + customRender() expect( screen.getByText( @@ -25,25 +21,8 @@ describe('NoProjectsNotice', () => { ).toBeInTheDocument() }) - test('fires onSwitchOrg when the inline link is clicked', () => { - const onSwitchOrg = vi.fn() - customRender( - - ) - - fireEvent.click(screen.getByText('Switch organization')) - - expect(onSwitchOrg).toHaveBeenCalledTimes(1) - }) - test('interpolates a different app name, so the copy is not hardcoded', () => { - customRender( - - ) + customRender() expect( screen.getByText( @@ -53,13 +32,7 @@ describe('NoProjectsNotice', () => { }) test('shows the same organization slug it was given', () => { - customRender( - - ) + customRender() expect(screen.getByText('No projects in northwind-traders')).toBeInTheDocument() expect(screen.queryByText('No projects in contoso-labs')).not.toBeInTheDocument() diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.tsx index eb98251d122..e5f2b025331 100644 --- a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.tsx +++ b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/NoProjectsNotice.tsx @@ -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 ( -

- Expecting to see your projects? - -

} /> diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.test.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.test.tsx index a5f604a2db9..2188f845e92 100644 --- a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.test.tsx +++ b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.test.tsx @@ -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) diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.tsx b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.tsx index 72fcc2d70db..19692e08394 100644 --- a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.tsx +++ b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/OAuthAppsAuthorizeScreen.tsx @@ -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 = ({
- + {isBlockedOnProjects && ( - + )} - {canProceed && isProjectScopingModeEnabled && (
- {canProceed ? ( - - ) : ( - - )} + + {!isSubmitting && ( + + + + + + No organization found. + + {(data ?? []).map((organization) => ( + { + onOrganizationChange(organization.slug) + setOpen(false) + }} + > + + {organization.name} + + ))} + + + + + +
+ ) +} diff --git a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/index.ts b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/index.ts index 1afe8c5be44..6464eec68f2 100644 --- a/apps/studio/components/interfaces/Organization/OAuthApps/Consent/index.ts +++ b/apps/studio/components/interfaces/Organization/OAuthApps/Consent/index.ts @@ -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' diff --git a/apps/studio/routes/authorize.tsx b/apps/studio/routes/authorize.tsx index 3c2b417e345..3d7f246b411 100644 --- a/apps/studio/routes/authorize.tsx +++ b/apps/studio/routes/authorize.tsx @@ -26,11 +26,10 @@ function APIAuthorizationPage() { <> {PAGE_TITLE} navigate({ href: destination })} /> )