mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 1 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). ## 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 Moves api-keys consumers that only need publishable keys from the V2 `API_GATEWAY_KEYS_READ` check and the transitional `useAPIKeys` / `useAPIKeysQuery` hooks onto a plain `GET /v1/projects/{ref}/api-keys` grant with `apiKeysQueryOptions`: - Docs: `Authentication` and `LangSelector` - Project API docs: `ProjectAPIDocs` (still only fetches while the panel is open) and `Content/Introduction` Without access, the docs keep their placeholder keys: - On the Data API docs page, the key picker in `LangSelector` is hidden and `Authentication` keeps the placeholder. There's no inline note. - In the API docs slideover, "Show keys" is disabled, with a tooltip saying the user needs additional permissions (or that the permission check failed). The old guards matched the endpoint annotation (`api_gateway_keys_read`). ## Testing - `Docs/ApiKeysGrants.platform.test.tsx`: `LangSelector` offers keys when granted, and hides the picker without requesting keys when denied or when the permission check fails. `Authentication` prefills the key when granted and keeps the placeholder otherwise. - `ProjectAPIDocs/ProjectAPIDocs.platform.test.tsx`: "Show keys" reveals the client key when granted; when denied or errored it's disabled with the matching tooltip, and no keys are requested. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
175 lines
5.9 KiB
TypeScript
175 lines
5.9 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { useParams } from 'common'
|
|
import { EyeOffIcon, KeyIcon } from 'lucide-react'
|
|
import { useMemo } from 'react'
|
|
import {
|
|
Button,
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuGroup,
|
|
DropdownMenuLabel,
|
|
DropdownMenuRadioGroup,
|
|
DropdownMenuRadioItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
ToggleGroup,
|
|
ToggleGroupItem,
|
|
} from 'ui'
|
|
|
|
import type { ShowApiKey } from '@/components/interfaces/Docs/Docs.types'
|
|
import { apiKeysQueryOptions } from '@/data/api-keys/api-keys-query'
|
|
import { useGrant } from '@/lib/grants'
|
|
|
|
const DEFAULT_KEY = { name: 'hide', key: 'SUPABASE_KEY' }
|
|
|
|
interface LangSelectorProps {
|
|
selectedLang: 'js' | 'bash'
|
|
selectedApiKey: ShowApiKey
|
|
setSelectedLang: (selectedLang: 'js' | 'bash') => void
|
|
setSelectedApiKey: (key: ShowApiKey) => void
|
|
}
|
|
|
|
export const LangSelector = ({
|
|
selectedLang,
|
|
selectedApiKey,
|
|
setSelectedLang,
|
|
setSelectedApiKey,
|
|
}: LangSelectorProps) => {
|
|
const { ref: projectRef } = useParams()
|
|
|
|
// Without access, the key picker is hidden and the commands keep their placeholder key
|
|
const apiKeysGrant = useGrant('GET /v1/projects/{ref}/api-keys', { projectRef })
|
|
const { data: apiKeys = [], isPending: isLoadingAPIKeys } = useQuery(
|
|
apiKeysQueryOptions({ projectRef, grant: apiKeysGrant.grant })
|
|
)
|
|
const canReadAPIKeys = apiKeysGrant.status === 'granted'
|
|
|
|
const legacyKeys = useMemo(() => apiKeys.filter(({ type }) => type === 'legacy'), [apiKeys])
|
|
const publishableKeys = useMemo(
|
|
() => apiKeys.filter(({ type }) => type === 'publishable'),
|
|
[apiKeys]
|
|
)
|
|
const secretKeys = useMemo(() => apiKeys.filter(({ type }) => type === 'secret'), [apiKeys])
|
|
|
|
return (
|
|
<div className="flex items-center gap-x-1">
|
|
<ToggleGroup
|
|
type="single"
|
|
value={selectedLang}
|
|
variant="outline"
|
|
onValueChange={(value) => {
|
|
if (value) setSelectedLang(value as 'js' | 'bash')
|
|
}}
|
|
size="sm"
|
|
className="flex-1 flex"
|
|
>
|
|
<ToggleGroupItem value="js" className="flex-1 px-2 py-1 h-7 text-xs">
|
|
JavaScript
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="bash" className="flex-1 px-2 py-1 h-7 text-xs">
|
|
Bash
|
|
</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
{selectedLang == 'bash' ? (
|
|
canReadAPIKeys &&
|
|
!isLoadingAPIKeys &&
|
|
apiKeys &&
|
|
apiKeys.length > 0 && (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button size="tiny" className="p-0 h-7 w-7">
|
|
{selectedApiKey.key === DEFAULT_KEY.key ? (
|
|
<EyeOffIcon size={12} strokeWidth={1.5} />
|
|
) : (
|
|
<KeyIcon size={12} strokeWidth={1.5} />
|
|
)}
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" side="bottom" className="w-48">
|
|
<DropdownMenuRadioGroup value={selectedApiKey.key}>
|
|
<DropdownMenuRadioItem
|
|
key="hide"
|
|
value={DEFAULT_KEY.key}
|
|
onClick={() => setSelectedApiKey(DEFAULT_KEY)}
|
|
>
|
|
Hide keys
|
|
</DropdownMenuRadioItem>
|
|
|
|
{publishableKeys.length > 0 && (
|
|
<>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuLabel>Publishable keys</DropdownMenuLabel>
|
|
{publishableKeys.map((key) => {
|
|
const value = key.api_key
|
|
return (
|
|
<DropdownMenuRadioItem
|
|
key={key.id}
|
|
value={value}
|
|
onClick={() =>
|
|
setSelectedApiKey({
|
|
name: `Publishable key: ${key.name}`,
|
|
key: value,
|
|
})
|
|
}
|
|
>
|
|
{key.name}
|
|
</DropdownMenuRadioItem>
|
|
)
|
|
})}
|
|
</>
|
|
)}
|
|
|
|
{secretKeys.length > 0 && (
|
|
<>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuLabel>Secret keys</DropdownMenuLabel>
|
|
{secretKeys.map((key) => {
|
|
const value = key.prefix + '...'
|
|
return (
|
|
<DropdownMenuRadioItem
|
|
key={key.id}
|
|
value={value}
|
|
onClick={() =>
|
|
setSelectedApiKey({ name: `Secret key: ${key.name}`, key: value })
|
|
}
|
|
>
|
|
{key.name}
|
|
</DropdownMenuRadioItem>
|
|
)
|
|
})}
|
|
</>
|
|
)}
|
|
|
|
{legacyKeys.length > 0 && (
|
|
<>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuGroup>
|
|
<DropdownMenuLabel>JWT-based legacy keys</DropdownMenuLabel>
|
|
{legacyKeys.map((key) => {
|
|
const value = key.api_key
|
|
return (
|
|
<DropdownMenuRadioItem
|
|
key={key.id}
|
|
value={value}
|
|
onClick={() =>
|
|
setSelectedApiKey({ name: `Legacy key: ${key.name}`, key: value })
|
|
}
|
|
>
|
|
{key.name}
|
|
</DropdownMenuRadioItem>
|
|
)
|
|
})}
|
|
</DropdownMenuGroup>
|
|
</>
|
|
)}
|
|
</DropdownMenuRadioGroup>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)
|
|
) : (
|
|
<div className="w-7 h-7" />
|
|
)}
|
|
</div>
|
|
)
|
|
}
|