Files
supabase/apps/studio/data/jit-db-access/jit-db-access-grant-mutation.ts
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

75 lines
2.2 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { jitDbAccessKeys } from './keys'
import { handleError, put, unguarded } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
type JitDbAccessGrantVariables = {
projectRef: string
userId: string
roles: Array<{
role: string
branches_only?: boolean
expires_at?: number // Unix timestamp in seconds per role
allowed_networks?: {
allowed_cidrs?: Array<{ cidr: string }>
allowed_cidrs_v6?: Array<{ cidr: string }>
}
}>
}
async function grantJitDbAccess({ projectRef, userId, roles }: JitDbAccessGrantVariables) {
if (!projectRef) throw new Error('projectRef is required')
if (!userId) throw new Error('userId is required')
if (!roles || roles.length === 0) throw new Error('At least one role is required')
const { data, error } = await put(`/v1/projects/{ref}/database/jit`, {
params: { path: { ref: projectRef } },
body: {
user_id: userId,
roles,
},
grant: unguarded.migrating,
})
if (error) handleError(error)
return data
}
type JitDbAccessGrantData = Awaited<ReturnType<typeof grantJitDbAccess>>
export const useJitDbAccessGrantMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<JitDbAccessGrantData, StudioError, JitDbAccessGrantVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<JitDbAccessGrantData, StudioError, JitDbAccessGrantVariables>({
mutationFn: (vars) => grantJitDbAccess(vars),
async onSuccess(data, variables, context, mutationContext) {
const { projectRef } = variables
await Promise.all([
queryClient.invalidateQueries({ queryKey: jitDbAccessKeys.members(projectRef) }),
queryClient.invalidateQueries({ queryKey: jitDbAccessKeys.status(projectRef) }),
])
await onSuccess?.(data, variables, context, mutationContext)
},
async onError(data, variables, context, mutationContext) {
if (onError === undefined) {
toast.error(`Failed to save member access rule: ${data.message}`)
} else {
onError(data, variables, context, mutationContext)
}
},
...options,
})
}