backen: jwt
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
// - ไม่ใช้ credentials: "include" อีกต่อไป
|
||||
// - เอา RootLayout/metadata ออก เพราะไฟล์เพจเป็น client component
|
||||
// - เพิ่มการอ่าน NEXT_PUBLIC_API_BASE และ error handling ให้ตรงกับ backend
|
||||
// - เพิ่มโหมดดีบัก เปิดด้วย NEXT_PUBLIC_DEBUG_AUTH=1
|
||||
|
||||
import { useState, useMemo, Suspense } from "react";
|
||||
import { useSearchParams, useRouter } from "next/navigation";
|
||||
@@ -25,6 +26,16 @@ import { Button } from "@/components/ui/button";
|
||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||
|
||||
const API_BASE = process.env.NEXT_PUBLIC_API_BASE?.replace(/\/$/, "") || "";
|
||||
const DEBUG =
|
||||
String(process.env.NEXT_PUBLIC_DEBUG_AUTH || "").trim() !== "" &&
|
||||
process.env.NEXT_PUBLIC_DEBUG_AUTH !== "0" &&
|
||||
process.env.NEXT_PUBLIC_DEBUG_AUTH !== "false";
|
||||
|
||||
function dlog(...args) {
|
||||
if (DEBUG && typeof window !== "undefined") {
|
||||
console.debug("[login]", ...args);
|
||||
}
|
||||
}
|
||||
|
||||
function LoginForm() {
|
||||
const router = useRouter();
|
||||
@@ -53,6 +64,12 @@ function LoginForm() {
|
||||
try {
|
||||
setSubmitting(true);
|
||||
|
||||
// ── DEBUG: ค่าเบื้องต้น
|
||||
dlog("API_BASE =", API_BASE || "(empty → จะเรียก path relative)");
|
||||
dlog("nextPath =", nextPath);
|
||||
dlog("remember =", remember);
|
||||
dlog("payload =", { username: "[hidden]", password: "[hidden]" });
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/auth/login`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
@@ -60,14 +77,30 @@ function LoginForm() {
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
dlog("response.status =", res.status);
|
||||
dlog("response.headers.content-type =", res.headers.get("content-type"));
|
||||
|
||||
let data = {};
|
||||
try {
|
||||
data = await res.json();
|
||||
} catch (e) {
|
||||
dlog("response.json() error =", e);
|
||||
}
|
||||
dlog("response.body =", data);
|
||||
|
||||
if (!res.ok) {
|
||||
// รองรับข้อความ error จาก backend เช่น INVALID_CREDENTIALS
|
||||
setErr(
|
||||
const msg =
|
||||
data?.error === "INVALID_CREDENTIALS"
|
||||
? "ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง"
|
||||
: data?.error || "เข้าสู่ระบบไม่สำเร็จ"
|
||||
);
|
||||
: data?.error || `เข้าสู่ระบบไม่สำเร็จ (HTTP ${res.status})`;
|
||||
dlog("login FAILED →", msg);
|
||||
setErr(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data?.token) {
|
||||
dlog("login FAILED → data.token not found");
|
||||
setErr("รูปแบบข้อมูลตอบกลับไม่ถูกต้อง (ไม่มี token)");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -76,6 +109,7 @@ function LoginForm() {
|
||||
storage.setItem("dms.token", data.token);
|
||||
storage.setItem("dms.refresh_token", data.refresh_token);
|
||||
storage.setItem("dms.user", JSON.stringify(data.user || {}));
|
||||
dlog("token stored in", remember ? "localStorage" : "sessionStorage");
|
||||
|
||||
// (ออปชัน) เผยแพร่ event ให้แท็บอื่นทราบ
|
||||
try {
|
||||
@@ -84,11 +118,14 @@ function LoginForm() {
|
||||
);
|
||||
} catch {}
|
||||
|
||||
dlog("navigating →", nextPath);
|
||||
router.replace(nextPath);
|
||||
} catch (e) {
|
||||
dlog("exception =", e);
|
||||
setErr("เชื่อมต่อเซิร์ฟเวอร์ไม่ได้ กรุณาลองใหม่");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
dlog("done");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,6 +220,12 @@ function LoginForm() {
|
||||
"เข้าสู่ระบบ"
|
||||
)}
|
||||
</Button>
|
||||
|
||||
{DEBUG ? (
|
||||
<p className="mt-2 text-xs text-slate-500">
|
||||
DEBUG: NEXT_PUBLIC_API_BASE = <code>{API_BASE || "(empty)"}</code>
|
||||
</p>
|
||||
) : null}
|
||||
</form>
|
||||
</CardContent>
|
||||
|
||||
@@ -251,4 +294,4 @@ function Spinner() {
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user