Files
supabase/apps/studio/data/oauth-apps/oauth-apps-revoke-mutation.ts
Gildas Garcia 4442d91d77 feat(studio): Wire org and users authorized apps list to backend (#53674)
## Description

Part of the work for OAuth apps with the scoped permissions. This PR
wires the org and users authorized apps lists to backend.
2026-11-19 21:40:12 +01:00

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,
})
}