251002 frontend/app/(auth)/login/page.jsx

This commit is contained in:
admin
2025-10-02 08:54:52 +07:00
parent cb4146fa35
commit dd48a26196

View File

@@ -66,9 +66,24 @@ function LoginForm() {
return;
}
// คุกกี้ (HttpOnly) ถูกตั้งด้วย Set-Cookie จาก backend แล้ว
dlog("login ok → redirect", nextPath);
router.replace(nextPath);
// ✅ ยืนยันว่าเซสชันพร้อมใช้งานก่อน (กัน redirect วน)
const me = await fetch(`${API_BASE}/api/auth/me`, {
method: "GET",
credentials: "include",
cache: "no-store",
}).then(r => r.ok ? r.json() : null).catch(() => null);
if (!me?.ok) {
setErr("ล็อกอินสำเร็จ แต่ตรวจสอบเซสชันไม่ผ่าน — โปรดลองใหม่");
return;
}
// ✅ ใช้ hard navigation ให้ SSR เห็นคุกกี้แน่นอน
if (typeof window !== "undefined") {
window.location.href = nextPath || "/dashboard";
} else {
router.replace(nextPath || "/dashboard");
}
} catch (e) {
dlog("exception =", e);
setErr("เชื่อมต่อเซิร์ฟเวอร์ไม่ได้ กรุณาลองใหม่");