mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 6 of 6 in a stack for [FE-4731](https://linear.app/supabase/issue/FE-4731/grants-rollout-api-keys-consumers-that-only-need-publishable-keys). Based on #52965. ## Stack 1. #52961 Docs and project API docs consumers 2. #52962 Edge functions and vector bucket examples 3. #52963 Integrations landing data 4. #52964 Connect sheet and project connection popover; remove the transitional api-keys hooks 5. #52965 `reveal` refinement for single api-key reads; API key pill 6. 👉 #52966 API key create and delete ## Changes Gates the api-key write endpoints (both require `api_gateway_keys_write`) on grants instead of `unguarded.migrating`: - `api-key-create-mutation` and `api-key-delete-mutation` take the grant in their variables. - `APIKeyDeleteDialog` uses the `DELETE` grant instead of its V2 check, and is disabled with a tooltip when denied or errored. `SecretAPIKeys` and `PublishableAPIKeys` pass the grant to the mutation. - `CreateNewAPIKeysButton` and the create publishable and secret key dialogs mint the `POST` grant. Their triggers and submit buttons are disabled with a tooltip unless granted. - `useConnectedResourceMutations`: the api-key delete call takes the grant. Without it, the mutation shows an error toast and throws. **New guard:** creating API keys had no UI guard. Users without `api_gateway_keys_write` could open the dialogs and get a 403; the controls are now disabled for them. The delete guard already matched the annotation. Out of scope: the legacy API key toggle and status (FE-4732). ## Testing - `CreateAPIKeyDialogs.platform.test.tsx` (renamed from `CreateAPIKeyDialogs.test.tsx`, the mutation mock removed): denied and permission-check error disable the triggers and the submit button. - `APIKeyDeleteDialog.platform.test.tsx`: granted, denied, and permission-check error. - `data/api-keys/api-key-write-mutations.test.ts`: create and delete send their requests with a grant. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { apiKeysKeys } from './keys'
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { GrantFor } from '@/lib/grants'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
export type APIKeyCreateGrant = GrantFor<'post', '/v1/projects/{ref}/api-keys'>
|
|
|
|
export type APIKeyCreateVariables = {
|
|
projectRef?: string
|
|
grant: APIKeyCreateGrant
|
|
name: string
|
|
description?: string
|
|
} & ({ type: 'publishable' } | { type: 'secret' })
|
|
|
|
export async function createAPIKey(payload: APIKeyCreateVariables) {
|
|
if (!payload.projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await post('/v1/projects/{ref}/api-keys', {
|
|
params: {
|
|
path: { ref: payload.projectRef },
|
|
query: {
|
|
reveal: 'false',
|
|
},
|
|
},
|
|
body: {
|
|
...(payload.type === 'secret'
|
|
? {
|
|
// secret_jwt_template: payload?.secret_jwt_template || null,
|
|
secret_jwt_template: {
|
|
role: 'service_role',
|
|
},
|
|
}
|
|
: {}),
|
|
|
|
type: payload.type,
|
|
name: payload.name,
|
|
description: payload.description || null,
|
|
},
|
|
grant: payload.grant,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type APIKeyCreateData = Awaited<ReturnType<typeof createAPIKey>>
|
|
|
|
export const useAPIKeyCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<APIKeyCreateData, StudioError, APIKeyCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<APIKeyCreateData, StudioError, APIKeyCreateVariables>({
|
|
mutationFn: (vars) => createAPIKey(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
|
|
await queryClient.invalidateQueries({ queryKey: apiKeysKeys.list(projectRef) })
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create API key: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|