diff --git a/frontend/app/_auth/client.js b/frontend/app/_auth/client.js
new file mode 100755
index 00000000..412bcf34
--- /dev/null
+++ b/frontend/app/_auth/client.js
@@ -0,0 +1,150 @@
+// frontend/app/page.jsx
+'use client';
+
+import { useEffect, useState } from 'react';
+import Link from 'next/link';
+import { useRouter } from 'next/navigation';
+import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
+import { Button } from '@/components/ui/button';
+import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs';
+import {
+ Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription,
+} from '@/components/ui/dialog';
+import { Label } from '@/components/ui/label';
+import { Input } from '@/components/ui/input';
+import { login as authLogin, logout as authLogout, me as fetchMe, credentials, authHeaders } from '@/app/_auth/client';
+
+export default function HomePage() {
+ const router = useRouter();
+ const [me, setMe] = useState(null);
+ const [loading, setLoading] = useState(true);
+
+ // login dialog states
+ const [open, setOpen] = useState(false);
+ const [username, setUsername] = useState('');
+ const [password, setPassword] = useState('');
+ const [submitting, setSubmitting] = useState(false);
+ const [err, setErr] = useState('');
+
+ // โหลดสถานะผู้ใช้แบบไม่ redirect
+ useEffect(() => {
+ let cancelled = false;
+ (async () => {
+ try {
+ const user = await fetchMe();
+ if (!cancelled) setMe(user);
+ } finally {
+ if (!cancelled) setLoading(false);
+ }
+ })();
+ return () => { cancelled = true; };
+ }, []);
+
+ async function handleLogin(e) {
+ e?.preventDefault();
+ setSubmitting(true);
+ setErr('');
+ try {
+ const user = await authLogin({ username, password });
+ if (!user) {
+ setErr('ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง');
+ return;
+ }
+ setMe(user);
+ setOpen(false);
+ router.push('/dashboard');
+ } catch {
+ setErr('เกิดข้อผิดพลาดในการเชื่อมต่อเซิร์ฟเวอร์');
+ } finally {
+ setSubmitting(false);
+ }
+ }
+
+ async function handleLogout() {
+ await authLogout().catch(() => {});
+ setMe(null);
+ }
+
+ if (loading) {
+ return (
+
+
+
+ {[...Array(3)].map((_, i) =>
)}
+
+
+ );
+ }
+
+ return (
+
+
+
Welcome to DMS
+
+ {/* แถบสถานะล็อกอิน + ปุ่ม */}
+
+ {me ? (
+ <>
+
สวัสดี, {me.first_name || me.username}
+
+
+ >
+ ) : (
+
+ )}
+
+
+
+ {/* เนื้อหาอื่น ๆ (เหมือนเดิม) */}
+
+
+ Overview
+ Activity
+
+
+
+
+
📑 RFAs24
+
📐 Drawings112
+
📤 Transmittals8
+
+
+
+
+
+
✔️ User editor01 uploaded Drawing D-2025-07
+
✔️ Transmittal T-2025-02 issued to Contractor
+
✔️ RFA R-2025-03 marked as Resolved
+
+
+
+
+
+
+ {!me && กรุณา Login ก่อน}
+
+
+ );
+}
diff --git a/frontend/app/page.jsx b/frontend/app/page.jsx
index b2c72ffe..93e1dedc 100755
--- a/frontend/app/page.jsx
+++ b/frontend/app/page.jsx
@@ -1,64 +1,142 @@
-// app/page.jsx
-"use client";
+// frontend/app/page.jsx
+'use client';
-import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
-import { Button } from "@/components/ui/button";
-import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
+import { useEffect, useState } from 'react';
+import Link from 'next/link';
+import { useRouter } from 'next/navigation';
+import { Button } from '@/components/ui/button';
+import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
+import {
+ Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription,
+} from '@/components/ui/dialog';
+import { Label } from '@/components/ui/label';
+import { Input } from '@/components/ui/input';
+
+// 👉 ใช้ driver เดิมของโปรเจ็กต์ (relative path + credentials: 'include')
+import cookieDriver from '@/app/_auth/drivers/cookieDriver';
export default function HomePage() {
+ const router = useRouter();
+
+ const [me, setMe] = useState(null);
+ const [loading, setLoading] = useState(true);
+
+ // dialog/login state
+ const [open, setOpen] = useState(false);
+ const [username, setUsername] = useState('');
+ const [password, setPassword] = useState('');
+ const [submitting, setSubmitting] = useState(false);
+ const [err, setErr] = useState('');
+
+ // โหลดสถานะผู้ใช้แบบไม่ redirect
+ useEffect(() => {
+ let cancelled = false;
+ (async () => {
+ try {
+ const res = await cookieDriver.me(); // ภายในยิง /api/auth/me ด้วย credentials: 'include'
+ if (!cancelled && res && (res.user || res.username)) {
+ const u = res.user ?? res;
+ setMe(u);
+ }
+ } finally {
+ if (!cancelled) setLoading(false);
+ }
+ })();
+ return () => { cancelled = true; };
+ }, []);
+
+ async function onSubmit(e) {
+ e?.preventDefault();
+ setErr('');
+ if (!username.trim() || !password) {
+ setErr('กรอกชื่อผู้ใช้และรหัสผ่านให้ครบ');
+ return;
+ }
+ try {
+ setSubmitting(true);
+ const ok = await cookieDriver.login({ username, password }); // ยิง /api/auth/login แบบ relative
+ if (!ok) {
+ setErr('ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง');
+ return;
+ }
+ // ตรวจ me อีกรอบให้ชัวร์ว่ามีคุกกี้แล้ว
+ const res = await cookieDriver.me();
+ const u = res?.user ?? res ?? { username };
+ setMe(u);
+ setOpen(false);
+ router.push('/dashboard');
+ } catch {
+ setErr('เชื่อมต่อเซิร์ฟเวอร์ไม่ได้');
+ } finally {
+ setSubmitting(false);
+ }
+ }
+
+ async function onLogout() {
+ try { await cookieDriver.logout?.(); } catch {}
+ setMe(null);
+ }
+
+ if (loading) {
+ return (
+
+
+
+ {[...Array(3)].map((_, i) =>
)}
+
+
+ );
+ }
+
return (
-
Welcome to DMS
+ {/* header */}
+
+
Welcome to DMS
+
+ {me ? (
+ <>
+
สวัสดี, {me.first_name || me.username}
+
+
+ >
+ ) : (
+
+ )}
+
+
-
-
- Overview
- Activity
-
+ {/* เนื้อหาย่อก่อนเข้าดาชบอร์ด */}
+
+
📑 RFAs—
+
📐 Drawings—
+
📤 Transmittals—
+
-
-
-
-
- 📑 RFAs
-
-
- 24
-
-
-
-
-
- 📐 Drawings
-
-
- 112
-
-
-
-
-
- 📤 Transmittals
-
-
- 8
-
-
-
-
-
-
-
-
- ✔️ User editor01 uploaded Drawing D-2025-07
-
-
✔️ Transmittal T-2025-02 issued to Contractor
-
✔️ RFA R-2025-03 marked as Resolved
-
-
-
-
-
+
+
+ {!me && กรุณา Login ก่อน}
+
);
}