Open-source personal finance manager. Self-hosted, privacy-first.
feat: add change password with current password verification
Tássio Noronha85e342cbb82c
diff --git a/frontend/src/components/app-layout.tsx b/frontend/src/components/app-layout.tsxindex f415b95b..8c52f3ee 100644--- a/frontend/src/components/app-layout.tsx+++ b/frontend/src/components/app-layout.tsx@@ -13,6 +13,7 @@ import {DropdownMenu,DropdownMenuContent,DropdownMenuItem,+ DropdownMenuSeparator,DropdownMenuTrigger,} from '@/components/ui/dropdown-menu'import { cn } from '@/lib/utils'@@ -35,8 +36,10 @@ import {BarChart3,Sun,Moon,+ KeyRound,} from 'lucide-react'import { usePrivacyMode } from '@/hooks/use-privacy-mode'+import { ChangePasswordDialog } from '@/components/change-password-dialog'const navItems = [{ key: 'dashboard', path: '/', icon: LayoutDashboard },@@ -65,6 +68,7 @@ export function AppLayout() {const [sidebarOpen, setSidebarOpen] = useState(false)const [accountsExpanded, setAccountsExpanded] = useState(true)const { privacyMode, togglePrivacyMode, mask } = usePrivacyMode()+ const [changePasswordOpen, setChangePasswordOpen] = useState(false)const showTour = user && !user.preferences?.onboarding_completed && !localStorage.getItem('onboarding_completed')@@ -115,7 +119,7 @@ export function AppLayout() {>{privacyMode ? <EyeOff size={18} /> : <Eye size={18} />}</button>- <UserMenu userInitial={userInitial} logout={logout} dark />+ <UserMenu userInitial={userInitial} logout={logout} onChangePassword={() => setChangePasswordOpen(true)} dark /></div></header>@@ -311,6 +315,14 @@ export function AppLayout() {</button></DropdownMenuTrigger><DropdownMenuContent align="end" className="w-48" side="top">+ <DropdownMenuItem+ onClick={() => setChangePasswordOpen(true)}+ className="flex items-center gap-2"+ >+ <KeyRound size={14} />+ {t('auth.changePassword')}+ </DropdownMenuItem>+ <DropdownMenuSeparator /><DropdownMenuItemonClick={logout}className="flex items-center gap-2 text-rose-600 focus:text-rose-600"@@ -332,11 +344,12 @@ export function AppLayout() {</div>{showTour && <OnboardingTour onComplete={handleTourComplete} />}+ <ChangePasswordDialog open={changePasswordOpen} onClose={() => setChangePasswordOpen(false)} /></div>)}-function UserMenu({ userInitial, logout, dark }: { userInitial: string; logout: () => void; dark?: boolean }) {+function UserMenu({ userInitial, logout, onChangePassword, dark }: { userInitial: string; logout: () => void; onChangePassword: () => void; dark?: boolean }) {const { t } = useTranslation()return (<DropdownMenu>@@ -350,6 +363,11 @@ function UserMenu({ userInitial, logout, dark }: { userInitial: string; logout:</Button></DropdownMenuTrigger><DropdownMenuContent align="end">+ <DropdownMenuItem onClick={onChangePassword} className="flex items-center gap-2">+ <KeyRound size={14} />+ {t('auth.changePassword')}+ </DropdownMenuItem>+ <DropdownMenuSeparator /><DropdownMenuItem onClick={logout} className="text-rose-600 focus:text-rose-600">{t('auth.logout')}</DropdownMenuItem>diff --git a/frontend/src/components/change-password-dialog.tsx b/frontend/src/components/change-password-dialog.tsxnew file mode 100644index 00000000..2bb0a6b7--- /dev/null+++ b/frontend/src/components/change-password-dialog.tsx@@ -0,0 +1,120 @@+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>+ )+}diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex 54caf6be..114c79a5 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -84,6 +84,10 @@ export const auth = {const { data } = await api.patch('/users/me', updates)return data},+ changePassword: async (password: string): Promise<User> => {+ const { data } = await api.patch('/users/me', { password })+ return data+ },}// Categoriesdiff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.jsonindex 361aa4e8..2de05d43 100644--- a/frontend/src/locales/en.json+++ b/frontend/src/locales/en.json@@ -25,7 +25,14 @@"loginDescription": "Sign in to access your account","invalidCredentials": "Invalid email or password","noAccount": "Don't have an account?",- "hasAccount": "Already have an account?"+ "hasAccount": "Already have an account?",+ "changePassword": "Change password",+ "newPassword": "New password",+ "passwordTooShort": "Password must be at least 8 characters",+ "currentPassword": "Current password",+ "currentPasswordWrong": "Current password is incorrect",+ "passwordChanged": "Password changed successfully",+ "passwordChangeError": "Failed to change password"},"dashboard": {"title": "Dashboard",diff --git a/frontend/src/locales/pt-BR.json b/frontend/src/locales/pt-BR.jsonindex 1fcb93fa..8769f345 100644--- a/frontend/src/locales/pt-BR.json+++ b/frontend/src/locales/pt-BR.json@@ -25,7 +25,14 @@"loginDescription": "Entre para acessar sua conta","invalidCredentials": "E-mail ou senha incorretos","noAccount": "Não tem uma conta?",- "hasAccount": "Já tem uma conta?"+ "hasAccount": "Já tem uma conta?",+ "changePassword": "Alterar senha",+ "newPassword": "Nova senha",+ "passwordTooShort": "A senha deve ter pelo menos 8 caracteres",+ "currentPassword": "Senha atual",+ "currentPasswordWrong": "Senha atual incorreta",+ "passwordChanged": "Senha alterada com sucesso",+ "passwordChangeError": "Erro ao alterar a senha"},"dashboard": {"title": "Painel",