4a673ebb28
Adds basePath config, withBase/appUrl helpers, base-path-aware cookies, OAuth redirects, manifest, service worker and cron runner. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
3.2 KiB
TypeScript
72 lines
3.2 KiB
TypeScript
"use client";
|
|
import { withBase } from "@/lib/base-path";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { MIN_PASSWORD_LENGTH } from "@/lib/auth/policy";
|
|
|
|
export default function ChangePasswordPage() {
|
|
const router = useRouter();
|
|
const [currentPassword, setCurrent] = useState("");
|
|
const [newPassword, setNew] = useState("");
|
|
const [confirm, setConfirm] = useState("");
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
async function submit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
if (newPassword !== confirm) return setError("รหัสผ่านใหม่ทั้งสองช่องไม่ตรงกัน");
|
|
setBusy(true);
|
|
setError(null);
|
|
const res = await fetch(withBase("/api/auth/change-password"), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ currentPassword, newPassword }),
|
|
});
|
|
const body = await res.json().catch(() => ({}));
|
|
setBusy(false);
|
|
if (!res.ok) return setError(body.error ?? "เปลี่ยนรหัสผ่านไม่สำเร็จ");
|
|
router.replace("/");
|
|
router.refresh();
|
|
}
|
|
|
|
async function logout() {
|
|
await fetch(withBase("/api/auth/logout"), { method: "POST" });
|
|
router.replace("/login");
|
|
}
|
|
|
|
return (
|
|
<main className="min-h-screen grid place-items-center px-4">
|
|
<form onSubmit={submit} className="card card-body flex flex-col gap-4 w-full max-w-[400px]" style={{ padding: 28 }}>
|
|
<div>
|
|
<h1 className="text-[22px] font-semibold">ตั้งรหัสผ่านใหม่</h1>
|
|
<p className="text-ink-3">ต้องตั้งรหัสผ่านของคุณเองก่อนใช้งาน (อย่างน้อย {MIN_PASSWORD_LENGTH} ตัวอักษร)</p>
|
|
</div>
|
|
<label className="field">
|
|
<span>รหัสผ่านปัจจุบัน</span>
|
|
<input className="input" type="password" autoComplete="current-password" required value={currentPassword} onChange={(e) => setCurrent(e.target.value)} />
|
|
</label>
|
|
<label className="field">
|
|
<span>รหัสผ่านใหม่</span>
|
|
<input className="input" type="password" autoComplete="new-password" required minLength={MIN_PASSWORD_LENGTH} value={newPassword} onChange={(e) => setNew(e.target.value)} />
|
|
</label>
|
|
<label className="field">
|
|
<span>ยืนยันรหัสผ่านใหม่</span>
|
|
<input className="input" type="password" autoComplete="new-password" required value={confirm} onChange={(e) => setConfirm(e.target.value)} />
|
|
</label>
|
|
{error && (
|
|
<p className="notice notice-red" role="alert">
|
|
{error}
|
|
</p>
|
|
)}
|
|
<button className="btn btn-primary btn-lg" disabled={busy}>
|
|
{busy ? "กำลังบันทึก…" : "บันทึกรหัสผ่าน"}
|
|
</button>
|
|
<button type="button" className="btn btn-ghost" onClick={logout}>
|
|
ออกจากระบบ
|
|
</button>
|
|
</form>
|
|
</main>
|
|
);
|
|
}
|