Initial role user app
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Eye, EyeOff, LockKeyhole, Smartphone } from "lucide-react";
|
||||
import { FormEvent, useState } from "react";
|
||||
|
||||
export function LoginForm() {
|
||||
const router = useRouter();
|
||||
const [error, setError] = useState("");
|
||||
const [pending, setPending] = useState(false);
|
||||
const [passwordVisible, setPasswordVisible] = useState(false);
|
||||
|
||||
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setError("");
|
||||
setPending(true);
|
||||
|
||||
const formData = new FormData(event.currentTarget);
|
||||
const response = await fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
username: String(formData.get("username") || "").trim(),
|
||||
password: String(formData.get("password") || "")
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const payload = (await response.json().catch(() => null)) as { message?: string } | null;
|
||||
setError(payload?.message || "登录失败,请检查手机号和密码");
|
||||
setPending(false);
|
||||
return;
|
||||
}
|
||||
|
||||
router.replace("/dashboard");
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="login-form" onSubmit={handleSubmit}>
|
||||
<label>
|
||||
<span>手机号</span>
|
||||
<div className="input-wrap">
|
||||
<Smartphone aria-hidden size={18} />
|
||||
<input
|
||||
autoComplete="username"
|
||||
defaultValue="13211111111"
|
||||
inputMode="tel"
|
||||
name="username"
|
||||
placeholder="请输入员工手机号"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</label>
|
||||
<label>
|
||||
<span>密码</span>
|
||||
<div className="input-wrap">
|
||||
<LockKeyhole aria-hidden size={18} />
|
||||
<input
|
||||
autoComplete="current-password"
|
||||
defaultValue="pw111111"
|
||||
name="password"
|
||||
placeholder="请输入登录密码"
|
||||
required
|
||||
type={passwordVisible ? "text" : "password"}
|
||||
/>
|
||||
<button
|
||||
aria-label={passwordVisible ? "隐藏密码" : "显示密码"}
|
||||
className="icon-button"
|
||||
onClick={() => setPasswordVisible((visible) => !visible)}
|
||||
type="button"
|
||||
>
|
||||
{passwordVisible ? <EyeOff aria-hidden size={18} /> : <Eye aria-hidden size={18} />}
|
||||
</button>
|
||||
</div>
|
||||
</label>
|
||||
{error ? <p className="form-error">{error}</p> : null}
|
||||
<button className="primary-button" disabled={pending} type="submit">
|
||||
{pending ? "登录中..." : "登录"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user