Files
Danny White 30a1b38da0 feat(studio): rename temporary access to database token access (#54282)
## Problem

The database access preview still uses “Temporary access” across its
settings and member rules.

## Solution

Use “Database token access” for the feature and “Member access” for
member rules. Update the preview, settings, dialogs, feedback, member
status messages and docs together. Move the guide to
`/guides/platform/token-access`, permanently redirect the old URL, and
preserve existing section anchors.

## Review instructions

1. Open [the Studio
preview](https://studio-staging-git-dnywh-database-token-access-naming-supabase.vercel.app),
then Feature previews and find **Database token access**. Enable the
preview for a supported project, then open [Database
Settings](https://studio-staging-git-dnywh-database-token-access-naming-supabase.vercel.app/project/_/database/settings).
2. Check the **Database token access** heading, **Allow token access**
switch and **Member access** table. Open **Add rule** and an existing
rule to check the sheet titles. Open a rule’s delete confirmation and
cancel it.
3. With an assigned role, check the access status above the member
table. As a member without management permission, check the access
notice uses the same feature name.
4. Open the [database token access guide in the docs
preview](https://docs-git-dnywh-database-token-access-naming-supabase.vercel.app/docs/guides/platform/token-access).
Check the guide and navigation use the new name, the dashboard
instructions match Studio, and the existing section links from the
member guide still work. Open `/docs/guides/platform/temporary-access`
and its `.md` variant to check they permanently redirect to the new
URLs.

| Before | After |
| --- | --- |
| <img width="726" height="367" alt="CleanShot 2026-11-30 at 16 06 00"
src="https://github.com/user-attachments/assets/be351f0c-d578-4289-8785-5c4e69a254a7"
/> | <img width="729" height="374" alt="CleanShot 2026-11-30 at 16 05
38"
src="https://github.com/user-attachments/assets/f02b470d-ef1a-4b6c-bdbb-20fe10b847c6"
/> |
2026-12-02 00:03:43 +00:00

360 lines
13 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useParams } from 'common'
import { parseAsBoolean, parseAsString, useQueryState } from 'nuqs'
import { useEffect, useMemo } from 'react'
import { useForm, useWatch } from 'react-hook-form'
import { toast } from 'sonner'
import {
Button,
Form,
FormControl,
FormField,
ScrollArea,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
Sheet,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { z } from 'zod'
import type {
JitExpiryMode,
JitMemberOption,
JitUserRuleDraft,
SheetMode,
} from './JitDbAccess.types'
import {
createDraft,
draftFromRule,
getAssignableJitRoleOptions,
getInvalidIpRangeRows,
mapJitMembersToUserRules,
serializeDraftRolesForGrantMutation,
} from './JitDbAccess.utils'
import { JitDbAccessRoleGrantFields } from './JitDbAccessRoleGrantFields'
import { DiscardChangesConfirmationDialog } from '@/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog'
import { InlineLink } from '@/components/ui/InlineLink'
import { useDatabaseRolesQuery } from '@/data/database-roles/database-roles-query'
import { useJitDbAccessGrantMutation } from '@/data/jit-db-access/jit-db-access-grant-mutation'
import { useJitDbAccessMembersQuery } from '@/data/jit-db-access/jit-db-access-members-query'
import { useProjectMembersQuery } from '@/data/projects/project-members-query'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useConfirmOnClose } from '@/hooks/ui/useConfirmOnClose'
import { DOCS_URL } from '@/lib/constants'
const grantSchema = z.object({
roleId: z.string(),
enabled: z.boolean(),
branchesOnly: z.boolean(),
expiryMode: z.custom<JitExpiryMode>(),
hasExpiry: z.boolean(),
expiry: z.string(),
ipRanges: z.array(z.object({ value: z.string() })),
})
function createJitRuleSchema(mode: SheetMode, membersWithRules: Set<string>) {
return z
.object({
memberId: z.string().min(1, 'Select a member.'),
grants: z.array(grantSchema),
})
.superRefine((data, ctx) => {
if (mode === 'add' && membersWithRules.has(data.memberId)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['memberId'],
message:
'This member already has an access rule. Edit their existing rule from the list.',
})
}
const enabledGrantCount = data.grants.filter((g) => g.enabled).length
if (enabledGrantCount === 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['grants'],
message: 'Select at least one role.',
})
return
}
data.grants.forEach((grant, grantIndex) => {
if (!grant.enabled) return
const invalidCidrs = new Set(getInvalidIpRangeRows(grant.ipRanges))
grant.ipRanges.forEach((ipRange, ipRangeIndex) => {
const value = ipRange.value.trim()
if (value.length === 0 || !invalidCidrs.has(value)) return
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['grants', grantIndex, 'ipRanges', ipRangeIndex, 'value'],
message: 'Please enter a valid CIDR range',
})
})
})
})
}
interface JitDbAccessRuleSheetProps {
memberOptions: JitMemberOption[]
membersWithRules: Set<string>
availableMembersForAddCount: number
}
/**
* [Joshen] Form schema can be further refactored to simplify
* It's weird that we're rendering the role options based on the form, when it's just based on
* the available database roles - should decouple
*/
export function JitDbAccessRuleSheet({
memberOptions,
membersWithRules,
availableMembersForAddCount,
}: JitDbAccessRuleSheetProps) {
const { ref: projectRef } = useParams()
const { data: project } = useSelectedProjectQuery()
const [isNewRule, setIsNewRule] = useQueryState('jit_new', parseAsBoolean.withDefault(false))
const [ruleIdToEdit, setRuleIdToEdit] = useQueryState('jit_edit', parseAsString)
// [Joshen] API uses DATABASE_JIT_WRITE atm to check against read access as well
// Opting to stay consistent, unless API changes to use DATABASE_JIT_READ
const { can: canUpdateJitDbAccess } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.DATABASE_JIT_WRITE
)
const { data: databaseRoles, isSuccess: isSuccessDatabaseRoles } = useDatabaseRolesQuery({
projectRef,
connectionString: project?.connectionString,
})
const roleOptions = useMemo(() => getAssignableJitRoleOptions(databaseRoles), [databaseRoles])
const roleIds = useMemo(() => roleOptions.map((role) => role.id), [roleOptions])
const { data: jitMembers, isSuccess: isSuccessJitMembers } = useJitDbAccessMembersQuery(
{ projectRef },
{ enabled: canUpdateJitDbAccess }
)
const { data: projectMembers, isSuccess: isSuccessProjectMembers } = useProjectMembersQuery({
projectRef,
})
const users = useMemo(
() => mapJitMembersToUserRules(jitMembers, projectMembers, roleOptions),
[jitMembers, projectMembers, roleOptions]
)
const user = users.find((x) => x.id === ruleIdToEdit)
const mode: SheetMode = !!user ? 'edit' : 'add'
const isDataReady = isSuccessDatabaseRoles && isSuccessJitMembers && isSuccessProjectMembers
const open = isNewRule || (!!ruleIdToEdit && !!user)
const defaultValues = !isNewRule && !!user ? draftFromRule(user, roleIds) : createDraft(roleIds)
const FormSchema = useMemo(
() => createJitRuleSchema(mode, membersWithRules),
[mode, membersWithRules]
)
const form = useForm<JitUserRuleDraft>({
defaultValues,
resolver: zodResolver(FormSchema),
})
const grants = useWatch({ control: form.control, name: 'grants' })
const onCloseSheet = () => {
setIsNewRule(false)
setRuleIdToEdit(null)
}
const {
confirmOnClose,
handleOpenChange,
modalProps: closeConfirmationModalProps,
} = useConfirmOnClose({
checkIsDirty: () => form.formState.isDirty,
onClose: onCloseSheet,
})
const { mutate: grantUserAccess, isPending: isSubmitting } = useJitDbAccessGrantMutation({
onSuccess: () => {
toast.success(mode === 'edit' ? 'Member access rule updated' : 'Member access rule created')
onCloseSheet()
},
onError: (error) => {
toast.error(
`Failed to ${mode === 'edit' ? 'update' : 'create'} member access rule: ${error.message}`
)
},
})
const updateGrant = (
roleId: string,
updater: (grant: JitUserRuleDraft['grants'][number]) => JitUserRuleDraft['grants'][number]
) => {
const nextGrants = grants.map((grant) => (grant.roleId === roleId ? updater(grant) : grant))
form.setValue('grants', nextGrants, { shouldDirty: true })
}
const handleSaveRule = (data: z.infer<typeof FormSchema>) => {
if (!projectRef) return console.error('Project ref is required')
const roles = serializeDraftRolesForGrantMutation(data)
if (roles.length === 0) return
grantUserAccess({ projectRef, userId: data.memberId, roles })
}
useEffect(() => {
if (!!ruleIdToEdit && isDataReady && !user) {
toast('Member access rule could not be found')
setRuleIdToEdit(null)
}
}, [isDataReady, ruleIdToEdit, setRuleIdToEdit, user])
useEffect(() => {
if (open && isDataReady) form.reset(defaultValues)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, isDataReady])
return (
<>
<Sheet open={open} onOpenChange={handleOpenChange}>
<SheetContent
showClose={false}
size="default"
className="flex h-full w-full max-w-full flex-col gap-0 sm:w-[560px]! sm:max-w-[560px]"
>
<SheetHeader>
<SheetTitle>
{mode === 'edit' ? 'Edit member access rule' : 'New member access rule'}
</SheetTitle>
<SheetDescription className="sr-only">
Choose which database roles this member can use and when their access expires.
</SheetDescription>
</SheetHeader>
<Form {...form}>
<ScrollArea className="flex-1 max-h-[calc(100vh-116px)]">
<div className="space-y-8 px-5 py-6 sm:px-6">
<FormField
control={form.control}
name="memberId"
render={({ field }) => (
<FormItemLayout layout="vertical" label="Member">
<FormControl>
<Select
value={field.value}
disabled={
mode === 'edit' || (mode === 'add' && availableMembersForAddCount === 0)
}
onValueChange={field.onChange}
>
<SelectTrigger>
<SelectValue placeholder="Select a member" />
</SelectTrigger>
<SelectContent>
{memberOptions.map((member) => (
<SelectItem key={member.id} value={member.id}>
{member.name ? (
<>
{member.name}{' '}
<span className="text-foreground-lighter">
({member.email})
</span>
</>
) : (
member.email
)}
</SelectItem>
))}
</SelectContent>
</Select>
</FormControl>
{mode === 'add' && availableMembersForAddCount === 0 && (
<p className="mt-2 text-foreground-lighter">
All project members already have access rules. Edit an existing rule from
the table above.
</p>
)}
</FormItemLayout>
)}
/>
<FormField
control={form.control}
name="grants"
render={() => (
<FormItemLayout
layout="vertical"
label="Roles and settings"
description={
<>
Use{' '}
<InlineLink
href={`${DOCS_URL}/guides/database/postgres/roles`}
className="decoration-foreground-muted"
>
custom Postgres roles
</InlineLink>{' '}
with narrow permissions to reduce the impact of direct database access.
</>
}
>
{grants.length === 0 ? (
<Admonition
type="note"
description="No assignable roles found."
className="bg-background"
/>
) : (
<div className="overflow-hidden rounded-md border">
{grants.map((grant, index) => (
<div key={grant.roleId} className={index > 0 ? 'border-t' : ''}>
<JitDbAccessRoleGrantFields
control={form.control}
grantIndex={index}
role={{ id: grant.roleId, label: grant.roleId }}
grant={grant}
onChange={(next) => updateGrant(grant.roleId, () => next)}
/>
</div>
))}
</div>
)}
</FormItemLayout>
)}
/>
</div>
</ScrollArea>
</Form>
<SheetFooter className="mt-auto w-full border-t py-4">
<Button onClick={confirmOnClose} disabled={isSubmitting}>
Cancel
</Button>
<Button
variant="primary"
onClick={form.handleSubmit(handleSaveRule)}
loading={isSubmitting}
>
{mode === 'edit' ? 'Save rule' : 'Create rule'}
</Button>
</SheetFooter>
</SheetContent>
</Sheet>
<DiscardChangesConfirmationDialog {...closeConfirmationModalProps} />
</>
)
}