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>({ 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> = ({ 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 */} !isUpdating && handleOpenChange(nextOpen)}> Reset password for "{role.name}" 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.
) }