Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
22 changes: 0 additions & 22 deletions src/constants/env.ts
Original file line number Diff line number Diff line change
@@ -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`,
},
};
48 changes: 28 additions & 20 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
};

Expand Down
Empty file removed src/lib/user-profile-fetch.ts
Empty file.