From dd48a26196d8a0d8ab8725b433094bd0390bf833 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 2 Oct 2025 08:54:52 +0700 Subject: [PATCH] 251002 frontend/app/(auth)/login/page.jsx --- frontend/app/(auth)/login/page.jsx | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/frontend/app/(auth)/login/page.jsx b/frontend/app/(auth)/login/page.jsx index 178dfe04..a2404532 100755 --- a/frontend/app/(auth)/login/page.jsx +++ b/frontend/app/(auth)/login/page.jsx @@ -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("เชื่อมต่อเซิร์ฟเวอร์ไม่ได้ กรุณาลองใหม่");