import { PermissionAction } from '@supabase/shared-types/out/constants' import { useParams } from 'common' import { useEffect, useState } from 'react' import { toast } from 'sonner' import { Button, Card, CardContent, Modal } from 'ui' import { Input } from 'ui-patterns/DataInputs/Input' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { PageSection, PageSectionContent, PageSectionDescription, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { PasswordStrengthBar } from '@/components/ui/PasswordStrengthBar' import { useDatabasePasswordResetMutation } from '@/data/database/database-password-reset-mutation' import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' import { useIsProjectActive, useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { DEFAULT_MINIMUM_PASSWORD_STRENGTH } from '@/lib/constants' import { passwordStrength, PasswordStrengthScore } from '@/lib/password-strength' import { generateStrongPassword } from '@/lib/project' const ResetDbPassword = ({ disabled = false }) => { const { ref } = useParams() const isProjectActive = useIsProjectActive() const { data: project } = useSelectedProjectQuery() const { can: canResetDbPassword } = useAsyncCheckPermissions( PermissionAction.UPDATE, 'projects', { resource: { project_id: project?.id, }, } ) const [showResetDbPass, setShowResetDbPass] = useState(false) const [password, setPassword] = useState('') const [passwordStrengthMessage, setPasswordStrengthMessage] = useState('') const [passwordStrengthWarning, setPasswordStrengthWarning] = useState('') const [passwordStrengthScore, setPasswordStrengthScore] = useState(0) const { mutate: resetDatabasePassword, isPending: isUpdatingPassword } = useDatabasePasswordResetMutation({ onSuccess: async () => { toast.success('Successfully updated database password') setShowResetDbPass(false) }, }) useEffect(() => { if (showResetDbPass) { setPassword('') setPasswordStrengthMessage('') setPasswordStrengthWarning('') setPasswordStrengthScore(0) } }, [showResetDbPass]) async function checkPasswordStrength(value: any) { const { message, warning, strength } = await passwordStrength(value) setPasswordStrengthScore(strength) setPasswordStrengthWarning(warning) setPasswordStrengthMessage(message) } const onDbPassChange = (e: any) => { const value = e.target.value setPassword(value) if (value == '') { setPasswordStrengthScore(-1) setPasswordStrengthMessage('') } else checkPasswordStrength(value) } const confirmResetDbPass = async () => { if (!ref) return console.error('Project ref is required') if (passwordStrengthScore >= DEFAULT_MINIMUM_PASSWORD_STRENGTH) { resetDatabasePassword({ ref, password }) } } function generatePassword() { const password = generateStrongPassword() setPassword(password) checkPasswordStrength(password) } return ( <> Database password Used for direct Postgres connections

Reset database password

The database password isn’t viewable after creation. Resetting it will break any existing connections.

setShowResetDbPass(true)} tooltip={{ content: { side: 'bottom', text: !canResetDbPassword ? 'You need additional permissions to reset the database password' : !isProjectActive ? 'Unable to reset database password as project is not active' : undefined, }, }} > Reset password
setShowResetDbPass(false)} > } > 0} aria-invalid={!!passwordStrengthWarning} type="password" placeholder="Type in a strong password" value={password} autoComplete="off" onChange={onDbPassChange} /> ) } export default ResetDbPassword