import { useState } from "react"; import { KeyRound, Loader2 } from "lucide-react"; import { useMutation } from "@tanstack/react-query"; import toast from "react-hot-toast"; import { api } from "@/services/api"; import { useAuth } from "@/auth/useAuth"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, Input, Label, } from "@edr/ui-common"; /** * Lets the signed-in backoffice user change their own password. The account * is logged out on success — the old token was issued under the old * password, and this forces a clean re-login rather than trusting the * server to keep the existing session valid. */ export function ChangePasswordCard() { const { logout } = useAuth(); const changePasswordMutation = useMutation( api.account.changePassword.mutationOptions(), ); const [open, setOpen] = useState(false); const [oldPassword, setOldPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [formError, setFormError] = useState(""); const closeDialog = () => { setOpen(false); setOldPassword(""); setNewPassword(""); setConfirmPassword(""); setFormError(""); }; const submit = () => { setFormError(""); if (!oldPassword || !newPassword || !confirmPassword) { setFormError("All fields are required."); return; } if (newPassword.length < 8) { setFormError("New password must be at least 8 characters."); return; } if (newPassword === oldPassword) { setFormError("New password must be different from the current one."); return; } if (newPassword !== confirmPassword) { setFormError("New password and confirmation do not match."); return; } changePasswordMutation.mutate( { oldPassword, newPassword, confirmPassword }, { onSuccess: () => { toast.success("Password changed. Please sign in again."); closeDialog(); setTimeout(logout, 1200); }, }, ); }; return ( Password Change the password for your account. !next && closeDialog()}> Change password You'll be signed out and asked to log in again once it's changed.
setOldPassword(e.target.value)} />
setNewPassword(e.target.value)} />
setConfirmPassword(e.target.value)} />
{formError &&

{formError}

}
); }