diff --git a/frontend/app/(auth)/login/page.jsx b/frontend/app/(auth)/login/page.jsx index a2404532..19712da8 100755 --- a/frontend/app/(auth)/login/page.jsx +++ b/frontend/app/(auth)/login/page.jsx @@ -67,14 +67,19 @@ function LoginForm() { } // ✅ ยืนยันว่าเซสชันพร้อมใช้งานก่อน (กัน 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("ล็อกอินสำเร็จ แต่ตรวจสอบเซสชันไม่ผ่าน — โปรดลองใหม่"); + // ✅ รอ session ให้พร้อมจริง (retry สูงสุด ~1.5s) + let me = null, ok = false; + for (let i = 0; i < 5; i++) { + 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) { ok = true; break; } + await new Promise(r => setTimeout(r, 300)); // เว้นระยะ 300ms + } + if (!ok) { + setErr("ล็อกอินสำเร็จ แต่ยังไม่เห็นเซสชันจากเซิร์ฟเวอร์ (ลองใหม่หรือตรวจคุกกี้)"); return; }