From 5af084f332ade7d2d2de3a2380225682e0ed706f Mon Sep 17 00:00:00 2001 From: Masatoshi Ogiwara Date: Mon, 21 Jul 2025 06:45:03 +0900 Subject: [PATCH] Revert "Replace mock login status with API integration" --- src/App.tsx | 2 +- src/constants/env.ts | 22 ---------------- src/context/AuthContext.tsx | 48 ++++++++++++++++++++--------------- src/lib/user-profile-fetch.ts | 0 4 files changed, 29 insertions(+), 43 deletions(-) delete mode 100644 src/lib/user-profile-fetch.ts diff --git a/src/App.tsx b/src/App.tsx index e179c79..81ac44a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,7 +7,7 @@ import FooterContainer from './components/common/Footer/FooterContainer'; const App: React.FC = () => { const { user } = useAuth(); - + const routes = useRoutes(user ? dashboardRoutes : authRoutes); return ( diff --git a/src/constants/env.ts b/src/constants/env.ts index f493636..e69de29 100644 --- a/src/constants/env.ts +++ b/src/constants/env.ts @@ -1,22 +0,0 @@ -export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL; - -export const API_ENDPOINTS = { - users: { - register: `${API_BASE_URL}/users/register`, - login: `${API_BASE_URL}/users/login`, - detail: (id: string) => `${API_BASE_URL}/users/show/${id}`, - update: (id: string) => `${API_BASE_URL}/users/${id}`, - delete: (id: string) => `${API_BASE_URL}/users/${id}`, - }, - posts: { - create: `${API_BASE_URL}/posts`, - list:(id: string) => `${API_BASE_URL}/users/${id}/posts`, - detail: (id: string) => `${API_BASE_URL}/posts/${id}`, - update: (id: string) => `${API_BASE_URL}/posts/${id}`, - delete: (id: string) => `${API_BASE_URL}/posts/${id}`, - }, - auth: { - logout: `${API_BASE_URL}/auth/logout`, - me: `${API_BASE_URL}/auth/me`, - }, -}; diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index c06dbe3..c463e4b 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -8,7 +8,7 @@ import { SetStateAction, } from 'react'; import { logoutUser } from '../lib/logout'; -import { User, fetchUserProfile } from '../lib/user'; +import { User } from '../lib/user'; type AuthContextType = { user: User | null; @@ -25,23 +25,33 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { useEffect(() => { const checkAuth = async () => { - const token = localStorage.getItem('token'); - const userId = localStorage.getItem('userId'); + try { - if (!token || !userId) { - setUser(null); - setLoading(false); - return; - } + const isMock = 'true'; - try { - const userProfile = await fetchUserProfile(userId, token); - setUser(userProfile); - } catch (error) { - console.error('Failed to fetch user profile:', error); + if (isMock) { + const mockUser = { + id: 1, + firstName: 'Mock', + lastName: 'User', + email: 'mock@example.com', + bio: 'This is a mock bio.', + location: 'Tokyo, Japan', + skills: ['React', 'TypeScript', 'Tailwind'], + profileImage: 'https://i.pravatar.cc/150?u=mock', + }; + setUser(mockUser); + } else { + const res = await fetch('/api/me'); + if (res.ok) { + const data = await res.json(); + setUser(data); + } else { + setUser(null); + } + } + } catch (err) { setUser(null); - localStorage.removeItem('token'); - localStorage.removeItem('userId'); } finally { setLoading(false); } @@ -52,13 +62,11 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { const logout = async () => { try { - await logoutUser(); // 通常のAPI logout - } catch (err) { - console.warn('Logout failed (ignored):', err); - } finally { + await logoutUser(); setUser(null); localStorage.removeItem('token'); - localStorage.removeItem('userId'); + } catch (err) { + alert(err); } }; diff --git a/src/lib/user-profile-fetch.ts b/src/lib/user-profile-fetch.ts deleted file mode 100644 index e69de29..0000000