Files
supabase/apps/studio/components/interfaces/Database/Roles/ResetRolePasswordDialog.tsx
Drew McLellan 1e94d386a4 feat(studio): add a reset password action for custom roles (#52506)
## What kind of change does this PR introduce?

Feature.

## What is the current behavior?

The Dashboard only offers a reset password action for the `postgres`
role (Database Settings). To change the password of a custom role, users
have to run `alter role ... password` in the SQL editor.

## What is the new behavior?

Each role row in Database > Roles has a "Reset password" item in its
actions menu. It opens a dialog with a warning that anything using the
old password will be disconnected, and the shared password field
(generate and copy). The password is required in this dialog.

- The item is disabled for system roles, and for roles that can't log
in, with a tooltip explaining why.
- It uses the existing role update mutation, so no platform endpoint is
involved.
- `RolePasswordField` gains `isOptional` and `layout` props so the
dialog can reuse it.

<img width="271" height="174" alt="Screenshot 2026-11-02 at 11 11 33"
src="https://github.com/user-attachments/assets/36f05fa0-e95c-440c-b6bb-4fadb466c2f6"
/>

<img width="543" height="414" alt="Screenshot 2026-11-02 at 11 11 17"
src="https://github.com/user-attachments/assets/87ba4f50-1315-4e07-b390-9c5b6beba129"
/>


## Testing

New `RoleRow` component tests cover the item being available for a
custom role that can log in, disabled for system roles and roles that
can't log in, the required-password validation, and a successful reset.

Resolves FE-4813.
2026-11-02 13:52:18 +00:00

121 lines
3.8 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useForm, type SubmitHandler } from 'react-hook-form'
import { toast } from 'sonner'
import {
Button,
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogSection,
DialogSectionSeparator,
DialogTitle,
Form,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import * as z from 'zod'
import { RolePasswordField } from './RolePasswordField'
import { DiscardChangesConfirmationDialog } from '@/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog'
import { useDatabaseRoleUpdateMutation } from '@/data/database-roles/database-role-update-mutation'
import type { PgRole } from '@/data/database-roles/database-roles-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { useConfirmOnClose } from '@/hooks/ui/useConfirmOnClose'
const FORM_ID = 'reset-role-password-form'
const FormSchema = z.object({
password: z.string().min(1, 'You must provide a password'),
})
interface ResetRolePasswordDialogProps {
role: PgRole
open: boolean
onOpenChange: (open: boolean) => void
}
export const ResetRolePasswordDialog = ({
role,
open,
onOpenChange,
}: ResetRolePasswordDialogProps) => {
const { data: project } = useSelectedProjectQuery()
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: { password: '' },
})
const { isDirty } = form.formState
const handleClose = () => {
form.reset()
onOpenChange(false)
}
const {
confirmOnClose,
handleOpenChange,
modalProps: discardChangesModalProps,
} = useConfirmOnClose({ checkIsDirty: () => isDirty, onClose: handleClose })
const { mutate: updateDatabaseRole, isPending: isUpdating } = useDatabaseRoleUpdateMutation({
onSuccess: () => {
toast.success(`Successfully reset the password for role "${role.name}"`)
handleClose()
},
})
const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = ({ password }) => {
if (!project) return
updateDatabaseRole({
projectRef: project.ref,
connectionString: project.connectionString,
id: role.id,
payload: { password },
})
}
return (
<>
{/* Dismissing mid-update would offer to discard a password that is still being saved */}
<Dialog open={open} onOpenChange={(nextOpen) => !isUpdating && handleOpenChange(nextOpen)}>
<DialogContent size="medium">
<DialogHeader>
<DialogTitle>Reset password for "{role.name}"</DialogTitle>
</DialogHeader>
<DialogSectionSeparator />
<DialogSection className="w-full space-y-8">
<Admonition type="warning" title="New connections must use the new password">
Existing sessions stay connected. Apps, ORMs, and tools that open a new connection as
this role will fail to sign in until you update them with the new password.
</Admonition>
<Form {...form}>
<form id={FORM_ID} onSubmit={form.handleSubmit(onSubmit)}>
<RolePasswordField isOptional={false} layout="vertical" />
</form>
</Form>
</DialogSection>
<DialogFooter>
<Button disabled={isUpdating} onClick={confirmOnClose}>
Cancel
</Button>
<Button
variant="primary"
type="submit"
form={FORM_ID}
disabled={!project}
loading={isUpdating}
>
Reset password
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<DiscardChangesConfirmationDialog
{...discardChangesModalProps}
description="Are you sure you want to discard your changes? The new password will not be saved."
/>
</>
)
}