mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Description Part of the work for OAuth apps with the scoped permissions. This PR wires the org and users authorized apps lists to backend.
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { del, handleError } from '../fetchers'
|
|
import { oauthAppsKeys } from './keys'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
export type OAuthAppRevokeVariables = {
|
|
slug: string
|
|
appId: string
|
|
}
|
|
|
|
export async function revokeOAuthApp({ slug, appId }: OAuthAppRevokeVariables) {
|
|
if (!slug) throw new Error('Organization slug is required')
|
|
if (!appId) throw new Error('App id is required')
|
|
const { error } = await del('/platform/organizations/{slug}/oauth/approvals/{app_id}', {
|
|
params: { path: { slug, app_id: appId } },
|
|
})
|
|
if (error) handleError(error)
|
|
}
|
|
|
|
export const useOAuthAppRevokeMutation = ({
|
|
onError,
|
|
onSuccess,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<void, StudioError, OAuthAppRevokeVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<void, StudioError, OAuthAppRevokeVariables>({
|
|
mutationFn: (vars) => revokeOAuthApp(vars),
|
|
async onSuccess(data, variables, context) {
|
|
// Revoking an app deletes every row for (app_id, organization_id), both kinds — the
|
|
// org's approvals overview, this app's grant list, and any member's own-grants view are
|
|
// all stale.
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: oauthAppsKeys.approvals(variables.slug) }),
|
|
queryClient.invalidateQueries({
|
|
queryKey: oauthAppsKeys.appMemberGrants(variables.slug, variables.appId),
|
|
}),
|
|
queryClient.invalidateQueries({ queryKey: oauthAppsKeys.grants() }),
|
|
])
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to revoke OAuth app: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|