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)
118 lines
3.8 KiB
TypeScript
118 lines
3.8 KiB
TypeScript
import { useParams } from 'common'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogBody,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from 'ui'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
|
|
import { getWriteGrantTooltip } from './APIKeys.utils'
|
|
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
|
|
import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation'
|
|
import { useGrant } from '@/lib/grants'
|
|
|
|
export const CreateNewAPIKeysButton = () => {
|
|
const { ref: projectRef } = useParams()
|
|
|
|
const [createKeysDialogOpen, setCreateKeysDialogOpen] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const createGrant = useGrant('POST /v1/projects/{ref}/api-keys', { projectRef })
|
|
|
|
const { mutateAsync: createAPIKey } = useAPIKeyCreateMutation({ onError: () => {} })
|
|
|
|
const handleCreateNewApiKeys = async () => {
|
|
if (!projectRef) return
|
|
if (!createGrant.grant) {
|
|
toast.error(
|
|
getWriteGrantTooltip(createGrant.status, 'create') ??
|
|
'Still checking permissions to create API keys. Try again in a moment.'
|
|
)
|
|
return
|
|
}
|
|
|
|
try {
|
|
setError(null)
|
|
|
|
// Create publishable key
|
|
try {
|
|
await createAPIKey({
|
|
projectRef,
|
|
grant: createGrant.grant,
|
|
type: 'publishable',
|
|
name: 'default',
|
|
})
|
|
} catch (error: any) {
|
|
setError(`Failed to create the default publishable key: ${error.message}`)
|
|
throw error
|
|
}
|
|
|
|
// Create secret key
|
|
try {
|
|
await createAPIKey({
|
|
projectRef,
|
|
grant: createGrant.grant,
|
|
type: 'secret',
|
|
name: 'default',
|
|
})
|
|
} catch (error: any) {
|
|
setError(
|
|
`The default publishable key was created, but the default secret key failed: ${error.message}`
|
|
)
|
|
throw error
|
|
}
|
|
|
|
setCreateKeysDialogOpen(false)
|
|
toast.success('Successfully created a new set of API keys!')
|
|
} catch (error) {
|
|
console.error('Failed to create API keys:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
return (
|
|
<AlertDialog open={createKeysDialogOpen} onOpenChange={setCreateKeysDialogOpen}>
|
|
<ButtonTooltip
|
|
variant="primary"
|
|
onClick={() => setCreateKeysDialogOpen(true)}
|
|
disabled={createGrant.status !== 'granted'}
|
|
tooltip={{
|
|
content: { side: 'bottom', text: getWriteGrantTooltip(createGrant.status, 'create') },
|
|
}}
|
|
>
|
|
Create new API keys
|
|
</ButtonTooltip>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Create new API keys</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This will create a default publishable key and a default secret key both named{' '}
|
|
<code className="break-keep!">default</code>. These keys are required to connect your
|
|
application to your Supabase project. Your existing legacy API keys (
|
|
<code className="break-keep!">anon</code> and{' '}
|
|
<code className="break-keep!">service_role</code>) are not affected and remain valid
|
|
until you disable them in a separate step.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
{error && (
|
|
<AlertDialogBody>
|
|
<Admonition type="destructive" title="Unable to create API keys" description={error} />
|
|
</AlertDialogBody>
|
|
)}
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction onClick={handleCreateNewApiKeys}>Create keys</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
)
|
|
}
|