ปรับ frontend

This commit is contained in:
admin
2025-10-01 13:53:46 +07:00
parent 5cac3bdabf
commit 2215633fb9
7 changed files with 269 additions and 213 deletions

View File

@@ -0,0 +1,34 @@
//File: frontend/app/_auth/drivers/cookieDriver.js
const API = process.env.NEXT_PUBLIC_API_BASE;
export default {
mode: 'cookie',
async login({ username, password }) {
const r = await fetch(`${API}/api/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include', // สำคัญสำหรับคุกกี้
body: JSON.stringify({ username, password })
});
if (!r.ok) throw new Error(`Login failed: ${r.status}`);
return true; // คุกกี้ถูกตั้งแล้วโดย backend
},
async me() {
const r = await fetch(`${API}/api/auth/me`, { credentials: 'include' });
if (!r.ok) return { ok: false };
return r.json();
},
async refresh() {
// ถ้า backend ออก access ใหม่ผ่าน refresh (ในคุกกี้) ก็เรียกได้
const r = await fetch(`${API}/api/auth/refresh`, {
method: 'POST',
credentials: 'include'
});
return r.ok;
},
authHeaders(h = {}) { return h; }, // คุกกี้ไม่ต้องใส่ Authorization
credentials() { return 'include'; },
async logout() {
await fetch(`${API}/api/auth/logout`, { method: 'POST', credentials: 'include' });
}
};