"use client"; import * as React from "react"; import { authClient } from "@/lib/auth-client"; import { recordAuditFromClient } from "@/actions/account"; import { PasswordInput, PasswordRequirements, passwordStrong } from "@/components/auth/password-input"; import { Button } from "@/components/ui/button"; import { Field, FieldError, Hint, Label } from "@/components/ui/label"; import { Alert } from "@/components/ui/alert"; export function ChangePasswordForm() { const [current, setCurrent] = React.useState(""); const [next, setNext] = React.useState(""); const [confirm, setConfirm] = React.useState(""); const [revokeOthers, setRevokeOthers] = React.useState(true); const [error, setError] = React.useState(null); const [done, setDone] = React.useState(false); const [loading, setLoading] = React.useState(false); const mismatch = confirm.length > 0 && confirm !== next; const canSubmit = current.length > 0 && passwordStrong(next) && next === confirm && next !== current; async function submit(e: React.FormEvent) { e.preventDefault(); setError(null); setDone(false); setLoading(true); const { error } = await authClient.changePassword({ currentPassword: current, newPassword: next, revokeOtherSessions: revokeOthers }); if (error) { setLoading(false); setError(error.status === 400 || error.status === 401 ? "Your current password is incorrect." : error.message ?? "Could not change the password. Try again."); return; } await recordAuditFromClient("password.changed", { revokedOtherSessions: revokeOthers }).catch(() => {}); setLoading(false); setDone(true); setCurrent(""); setNext(""); setConfirm(""); } return (
{error ? {error} : null} {done ? Password changed.{revokeOthers ? " Other devices have been signed out." : ""} : null} setCurrent(e.target.value)} required /> setNext(e.target.value)} required /> {next && current && next === current ? Choose a password different from the current one. : null} setConfirm(e.target.value)} required aria-invalid={mismatch} /> {mismatch ? "Passwords do not match." : null}
); }