fix: tailwind v4 postcss, auth-server session, eslint cleanups
This commit is contained in:
87
frontend/lib/auth.js
Normal file → Executable file
87
frontend/lib/auth.js
Normal file → Executable file
@@ -1,82 +1,41 @@
|
||||
// frontend/lib/auth.js
|
||||
// frontend/lib/auth.js
|
||||
|
||||
'use client';
|
||||
|
||||
import { createContext, useState, useContext, useEffect } from 'react';
|
||||
import api from './api';
|
||||
// 1. Import cookieDriver ที่คุณมีอยู่แล้ว ซึ่งเป็นวิธีที่ถูกต้อง
|
||||
import { cookieDriver } from '@/app/_auth/drivers/cookieDriver';
|
||||
import { createContext, useContext, useEffect, useState, useCallback } from "react";
|
||||
|
||||
const AuthContext = createContext(null);
|
||||
|
||||
const COOKIE_NAME = "access_token";
|
||||
const AuthContext = createContext({
|
||||
user: null,
|
||||
isAuthenticated: false,
|
||||
loading: true,
|
||||
logout: () => {},
|
||||
});
|
||||
|
||||
export function AuthProvider({ children }) {
|
||||
const [user, setUser] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const initializeAuth = async () => {
|
||||
// 2. อ่าน token จาก cookie ด้วย cookieDriver.get()
|
||||
const token = cookieDriver.get(COOKIE_NAME);
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
api.defaults.headers.Authorization = `Bearer ${token}`;
|
||||
// สมมติว่ามี endpoint /auth/me สำหรับดึงข้อมูลผู้ใช้
|
||||
const response = await api.get('/auth/me');
|
||||
setUser(response.data.user || response.data); // รองรับทั้งสองรูปแบบ
|
||||
} catch (error) {
|
||||
console.error("Failed to initialize auth from cookie:", error);
|
||||
cookieDriver.remove(COOKIE_NAME);
|
||||
delete api.defaults.headers.Authorization;
|
||||
}
|
||||
}
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
initializeAuth();
|
||||
fetch("/api/auth/me", { credentials: "include" })
|
||||
.then((res) => (res.ok ? res.json() : null))
|
||||
.then((data) => setUser(data?.user ?? null))
|
||||
.finally(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
const login = async (credentials) => {
|
||||
const response = await api.post('/auth/login', credentials);
|
||||
const { token, user } = response.data;
|
||||
|
||||
// 3. ตั้งค่า token ใน cookie ด้วย cookieDriver.set()
|
||||
cookieDriver.set(COOKIE_NAME, token, { expires: 7, secure: true, sameSite: 'strict' });
|
||||
api.defaults.headers.Authorization = `Bearer ${token}`;
|
||||
setUser(user);
|
||||
return user;
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
// 4. ลบ token ออกจาก cookie ด้วย cookieDriver.remove()
|
||||
cookieDriver.remove(COOKIE_NAME);
|
||||
delete api.defaults.headers.Authorization;
|
||||
setUser(null);
|
||||
window.location.href = '/login';
|
||||
};
|
||||
|
||||
const value = {
|
||||
user,
|
||||
isAuthenticated: !!user,
|
||||
loading,
|
||||
login,
|
||||
logout
|
||||
};
|
||||
const logout = useCallback(async () => {
|
||||
try {
|
||||
await fetch("/api/auth/logout", { method: "POST", credentials: "include" });
|
||||
} finally {
|
||||
window.location.href = "/login";
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>
|
||||
{!loading && children}
|
||||
<AuthContext.Provider value={{ user, isAuthenticated: !!user, loading, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
const context = useContext(AuthContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useAuth must be used within an AuthProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
export function useAuth() {
|
||||
return useContext(AuthContext);
|
||||
}
|
||||
Reference in New Issue
Block a user