Open-source personal finance manager. Self-hosted, privacy-first.
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { useAuth } from '@/contexts/auth-context'
import { auth } from '@/lib/api'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogFooter,
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
interface ChangePasswordDialogProps {
open: boolean
onClose: () => void
}
export function ChangePasswordDialog({ open, onClose }: ChangePasswordDialogProps) {
const { t } = useTranslation()
const { user } = useAuth()
const [currentPassword, setCurrentPassword] = useState('')
const [newPassword, setNewPassword] = useState('')
const [confirmPassword, setConfirmPassword] = useState('')
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setError('')
if (newPassword.length < 8) {
setError(t('auth.passwordTooShort'))
return
}
if (newPassword !== confirmPassword) {
setError(t('setup.passwordMismatch'))
return
}
setLoading(true)
try {
// Verify current password by attempting login
await auth.login(user!.email, currentPassword)
await auth.changePassword(newPassword)
toast.success(t('auth.passwordChanged'))
handleClose()
} catch {
setError(t('auth.currentPasswordWrong'))
} finally {
setLoading(false)
}
}
const handleClose = () => {
setCurrentPassword('')
setNewPassword('')
setConfirmPassword('')
setError('')
onClose()
}
return (
<Dialog open={open} onOpenChange={(v) => !v && handleClose()}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{t('auth.changePassword')}</DialogTitle>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="current-password">{t('auth.currentPassword')}</Label>
<Input
id="current-password"
type="password"
value={currentPassword}
onChange={(e) => setCurrentPassword(e.target.value)}
autoComplete="current-password"
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="new-password">{t('auth.newPassword')}</Label>
<Input
id="new-password"
type="password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
autoComplete="new-password"
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="confirm-password">{t('setup.confirmPassword')}</Label>
<Input
id="confirm-password"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
autoComplete="new-password"
required
/>
</div>
{error && <p className="text-sm text-destructive">{error}</p>}
<DialogFooter>
<Button type="button" variant="outline" onClick={handleClose}>
{t('common.cancel')}
</Button>
<Button type="submit" disabled={loading}>
{loading ? t('common.loading') : t('common.save')}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
)
}