Files
FL-Akademie/components/password-change-form.tsx

64 lines
1.8 KiB
TypeScript

"use client";
import { useState } from "react";
export function PasswordChangeForm() {
const [currentPassword, setCurrentPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [msg, setMsg] = useState<string | null>(null);
const [err, setErr] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
async function onSubmit(e: React.FormEvent) {
e.preventDefault();
setLoading(true);
setErr(null);
setMsg(null);
const res = await fetch("/api/portal/password", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ currentPassword, newPassword }),
});
const data = (await res.json().catch(() => ({}))) as { error?: string };
setLoading(false);
if (!res.ok) {
setErr(data.error ?? "Änderung fehlgeschlagen.");
return;
}
setMsg("Passwort wurde aktualisiert.");
setCurrentPassword("");
setNewPassword("");
}
return (
<form className="panel form" onSubmit={onSubmit}>
<label>
Aktuelles Passwort
<input
type="password"
autoComplete="current-password"
value={currentPassword}
onChange={(e) => setCurrentPassword(e.target.value)}
required
/>
</label>
<label>
Neues Passwort (mind. 8 Zeichen)
<input
type="password"
autoComplete="new-password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
required
minLength={8}
/>
</label>
{err ? <p className="error">{err}</p> : null}
{msg ? <p className="muted">{msg}</p> : null}
<button type="submit" className="btn btn-primary" disabled={loading}>
{loading ? "…" : "Passwort speichern"}
</button>
</form>
);
}