Initial role user app

This commit is contained in:
湛兮
2026-06-02 14:46:39 +08:00
commit 003dc60111
62 changed files with 7835 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
"use client";
import { KeyRound } from "lucide-react";
import { FormEvent, useState } from "react";
import type { ApiEnvelope } from "@/lib/types";
async function readError(response: Response) {
const payload = (await response.json().catch(() => null)) as Partial<ApiEnvelope<unknown>> | null;
return payload?.message || "修改密码失败";
}
export function PasswordChangeForm() {
const [pending, setPending] = useState(false);
const [error, setError] = useState("");
const [message, setMessage] = useState("");
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setError("");
setMessage("");
setPending(true);
const form = event.currentTarget;
const formData = new FormData(form);
const oldPassword = String(formData.get("oldPassword") || "");
const newPassword = String(formData.get("newPassword") || "");
const confirmPassword = String(formData.get("confirmPassword") || "");
if (newPassword !== confirmPassword) {
setError("两次输入的新密码不一致");
setPending(false);
return;
}
const response = await fetch("/api/auth/me/password", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ oldPassword, newPassword })
});
if (!response.ok) {
setError(await readError(response));
setPending(false);
return;
}
form.reset();
setMessage("密码已修改");
setPending(false);
}
return (
<form className="form-stack" onSubmit={handleSubmit}>
<label>
<span>旧密码</span>
<div className="input-wrap">
<KeyRound aria-hidden size={18} />
<input autoComplete="current-password" minLength={8} name="oldPassword" required type="password" />
</div>
</label>
<label>
<span>新密码</span>
<div className="input-wrap">
<KeyRound aria-hidden size={18} />
<input autoComplete="new-password" minLength={8} name="newPassword" required type="password" />
</div>
</label>
<label>
<span>确认新密码</span>
<div className="input-wrap">
<KeyRound aria-hidden size={18} />
<input autoComplete="new-password" minLength={8} name="confirmPassword" required type="password" />
</div>
</label>
{error ? <p className="form-error">{error}</p> : null}
{message ? <p className="form-success">{message}</p> : null}
<button className="primary-button" disabled={pending} type="submit">
<KeyRound aria-hidden size={18} />
{pending ? "修改中..." : "修改密码"}
</button>
</form>
);
}