import { useState } from "react"; import { BackLink } from "@/client/components/ui/back-link"; import { Button } from "@/client/components/ui/button"; import { Card } from "@/client/components/ui/card"; import { Input } from "@/client/components/ui/input"; import { PageError } from "@/client/components/ui/page-error"; import { PageHeader } from "@/client/components/ui/page-header"; import { Skeleton } from "@/client/components/ui/skeleton"; import { useRequireSession } from "@/client/hooks/use-auth-guard"; import { authClient } from "@/client/lib/auth-client"; const AccountPasswordPageSkeleton = () => (
); export const AccountPasswordPage = () => { const session = useRequireSession("/account/password"); const [current, setCurrent] = useState(""); const [next, setNext] = useState(""); const [confirm, setConfirm] = useState(""); const [pending, setPending] = useState(false); const [err, setErr] = useState(null); const [success, setSuccess] = useState(false); if (session.isPending || !session.data) { return ; } const submit = async (e: React.FormEvent) => { e.preventDefault(); setErr(null); setSuccess(false); if (next !== confirm) { setErr("New passwords don't match."); return; } if (next.length < 12) { setErr("New password must be at least 12 characters."); return; } setPending(true); try { const res = await authClient.changePassword({ currentPassword: current, newPassword: next, revokeOtherSessions: true, }); if (res.error) { setErr(res.error.message ?? "Could not change password."); return; } setCurrent(""); setNext(""); setConfirm(""); setSuccess(true); } finally { setPending(false); } }; return (
Account setErr(null)} />
setCurrent(e.target.value)} /> setNext(e.target.value)} /> setConfirm(e.target.value)} onBlur={() => { // Catch the mismatch before submit so the user notices while // their attention is still on the field. if (confirm && next && confirm !== next) { setErr("New passwords don't match."); } }} /> {success ? (

Password updated. Other sessions have been signed out.

) : null}
); };