Files
supabase/apps/studio/components/interfaces/ProjectAPIDocs/Content/Introduction.tsx
T
fffb1e23ca feat: implement profile permissions v2 api (#50966)
## Problem

Migrating Studio's permission checks from the old RLS json logic to
openfga backed permissions.

### New hooks and helpers

- `usePermissionsQueryV2` fetches the new profile openfga permisisons 
- `useAsyncCheckPermissionsV2` the replacement for the old check hook;
takes an FGA permission string (or a list)
- `FGA_PERMISSIONS` permission constants derived from
`@supabase/shared-types`. At the moment, the hook also accepts plain
strings for permissions not yet published in the package
- `useAsyncCheckUserContentPermissions` new hook for permission checks
on user content (SQL snippets, reports, log snippets). This wraps
``useAsyncCheckPermissionsV2` and adds existing content level rules on
top of the fga permission check

### Notes

- ~~Need api to support new permissions: project_operations_write,
billing_read/write, audit_logs_read~~ [DONE]
- ~~Still need to support: organization_config_read/write, auth hooks
and users permissions~~ [DONE]

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2026-10-21 06:54:16 -04:00

140 lines
5.0 KiB
TypeScript

import { useParams } from 'common'
import { CopyIcon } from 'lucide-react'
import { useEffect, useState } from 'react'
import { Button, copyToClipboard } from 'ui'
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import ContentSnippet from '../ContentSnippet'
import { DOCS_CONTENT } from '../ProjectAPIDocs.constants'
import type { ContentProps } from './Content.types'
import { useAPIKeys } from '@/data/api-keys/api-keys-query'
import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useTrack } from '@/lib/telemetry/track'
export const Introduction = ({ showKeys, language, apikey, endpoint }: ContentProps) => {
const { ref } = useParams()
const { can: canReadAPIKeys } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.API_GATEWAY_KEYS_READ
)
const { data: apiKeysData } = useAPIKeys({ projectRef: ref }, { enabled: canReadAPIKeys })
useProjectSettingsV2Query({ projectRef: ref })
const { anonKey, serviceKey } = apiKeysData ?? {}
const track = useTrack()
const [copied, setCopied] = useState<'anon' | 'service'>()
useEffect(() => {
if (copied !== undefined) setTimeout(() => setCopied(undefined), 2000)
}, [copied])
const anonApiKey = anonKey?.api_key
const serviceApiKey = serviceKey?.api_key ?? 'SUPABASE_CLIENT_SERVICE_KEY'
return (
<>
<ContentSnippet
selectedLanguage={language}
apikey={apikey}
endpoint={endpoint}
snippet={DOCS_CONTENT.init}
>
<div className="px-4 space-y-6">
<div className="flex flex-col space-x-4 mt-8">
<FormItemLayout isReactForm={false} layout="horizontal" label="Project URL">
<Input disabled readOnly copy size="small" value={endpoint} className="w-full" />
</FormItemLayout>
</div>
<div className="flex flex-col space-x-4">
<FormItemLayout
isReactForm={false}
layout="horizontal"
label="Client API key"
description="This key is safe to use in a browser if you have enabled Row Level Security (RLS) for your tables and configured policies."
>
<Input
disabled
readOnly
size="small"
value={showKeys ? apikey : 'Reveal API keys via dropdown in the header'}
actions={[
<Button
key="copy"
icon={<CopyIcon />}
onClick={() => {
setCopied('anon')
copyToClipboard(anonApiKey ?? 'SUPABASE_CLIENT_ANON_KEY')
track('api_docs_code_copy_button_clicked', {
title: 'Client API key',
selectedLanguage: language,
})
}}
>
{copied === 'anon' ? 'Copied' : 'Copy'}
</Button>,
]}
/>
</FormItemLayout>
</div>
<div className="flex flex-col space-x-4">
<FormItemLayout
isReactForm={false}
layout="horizontal"
label="Service key"
description={
<p>
This key has the ability to bypass Row Level Security.{' '}
<span className="text-amber-900">Never share it publicly.</span>
</p>
}
>
<Input
disabled
readOnly
size="small"
value={
showKeys
? (serviceApiKey ?? 'SUPABASE_CLIENT_SERVICE_KEY')
: 'Reveal API keys via dropdown in the header'
}
actions={[
<Button
key="copy"
icon={<CopyIcon />}
onClick={() => {
setCopied('service')
copyToClipboard(serviceApiKey)
track('api_docs_code_copy_button_clicked', {
title: 'Service key',
selectedLanguage: language,
})
}}
>
{copied === 'service' ? 'Copied' : 'Copy'}
</Button>,
]}
/>
</FormItemLayout>
</div>
</div>
</ContentSnippet>
<ContentSnippet
selectedLanguage={language}
apikey={apikey}
endpoint={endpoint}
snippet={DOCS_CONTENT.clientApiKeys}
/>
<ContentSnippet
selectedLanguage={language}
apikey={showKeys ? serviceApiKey : 'SUPABASE_CLIENT_SERVICE_KEY'}
endpoint={endpoint}
snippet={DOCS_CONTENT.serviceApiKeys}
/>
</>
)
}