Files
supabase/apps/studio/data/oauth/oauth-app-create-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

83 lines
2.2 KiB
TypeScript

import type { OAuthScope } from '@supabase/shared-types/out/constants'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { components } from 'api-types'
import { toast } from 'sonner'
import { oauthAppKeys } from './keys'
import { handleError, post } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomMutationOptions } from '@/types'
export type OAuthAppCreateVariables = {
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 type OAuthAppCreateResponse = components['schemas']['CreateOAuthAppResponse_Output']
export async function createOAuthApp({
slug,
name,
website,
icon,
scopes,
redirect_uris,
grant_kind,
project_scoping_mode,
allow_partial_grants,
}: OAuthAppCreateVariables) {
const { data, error } = await post('/platform/organizations/{slug}/oauth/apps', {
params: { path: { slug } },
body: {
name,
website,
icon,
scopes,
redirect_uris,
grant_kind,
project_scoping_mode,
allow_partial_grants,
},
})
if (error) handleError(error)
return data
}
type OAuthAppCreateData = Awaited<ReturnType<typeof createOAuthApp>>
export const useOAuthAppCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<OAuthAppCreateData, StudioError, OAuthAppCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<OAuthAppCreateData, StudioError, OAuthAppCreateVariables>({
mutationFn: (vars) => createOAuthApp(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 create OAuth application: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}