Files
supabase/apps/studio/data/oauth/oauth-app-update-mutation.ts
Gildas Garcia dde52ce6e8 feat(studio): update oauth publishable apps to include new options (#53355)
## Description

Part of the work for OAuth apps with the scoped permissions.
This PR updates the publishable apps UI to actually submit the new
options

> [!NOTE]  
> The backend may reject the API call if your organization isn't
enrolled in scoped oauth apps. That does not prevent to check the
payload is correct

## Review instructions

- Open _Organizations settings/OAuth Apps_
- Click the _Publish OAuth app_ button
- Check that both options in the _Authorization_ section require a
confirmation with a checkbox
- Before submitting, open your devtool
- Submit the app
- Check that the API route
`https://api.supabase.green/platform/organizations/[org_slug]/oauth/apps`
was called with the options you selected:
- if _Use the permissions of the person who connects_ was checked:
`grant_kind: "member_bound"`, otherwise `grant_kind:
"organization_bound"`
- If _Ask for specific projects_ was checked: `project_scoping_mode:
true` otherwise `project_scoping_mode: false`
2026-11-17 16:35:32 +01:00

87 lines
2.4 KiB
TypeScript

import type { OAuthScope } from '@supabase/shared-types/out/constants'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { oauthAppKeys } from './keys'
import { handleError, put } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomMutationOptions } from '@/types'
export type OAuthAppUpdateVariables = {
id: string
slug: string
name: string
website: string
icon?: string
scopes: OAuthScope[]
redirect_uris: string[]
grant_kind?: 'organization_bound' | 'member_bound'
project_scoping_mode?: boolean
allow_partial_grants?: boolean
}
export async function updateOAuthApp({
id,
slug,
name,
website,
icon,
scopes,
redirect_uris,
grant_kind,
project_scoping_mode,
allow_partial_grants,
}: OAuthAppUpdateVariables) {
if (!id) throw new Error('OAuth app ID is required')
if (!slug) throw new Error('Organization slug is required')
if (!name) throw new Error('OAuth app name is required')
if (!website) throw new Error('OAuth app URL is required')
if (!redirect_uris || redirect_uris.length === 0) throw new Error('Redirect URIs are required')
const { data, error } = await put(`/platform/organizations/{slug}/oauth/apps/{id}`, {
params: { path: { id, slug } },
body: {
name,
website,
icon,
scopes,
redirect_uris,
grant_kind,
project_scoping_mode,
allow_partial_grants,
},
})
if (error) throw handleError(error)
return data
}
type OAuthAppUpdateData = Awaited<ReturnType<typeof updateOAuthApp>>
export const useOAuthAppUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<OAuthAppUpdateData, StudioError, OAuthAppUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<OAuthAppUpdateData, StudioError, OAuthAppUpdateVariables>({
mutationFn: (vars) => updateOAuthApp(vars),
async onSuccess(data, variables, context) {
const { slug } = variables
await queryClient.invalidateQueries({ queryKey: oauthAppKeys.oauthApps(slug) })
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update application: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}