This commit is contained in:
ondal
2025-02-13 15:47:22 +09:00
commit bb7ce2d988
53 changed files with 19663 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
// src/contexts/AuthContext.js
import React, { createContext, useContext, useState, useEffect } from 'react';
import { authApi, setAuthToken, removeAuthToken } from '../services/api';
const AuthContext = createContext(null);
export const AuthProvider = ({ children }) => {
const [currentUser, setCurrentUser] = useState(null);
const [authInitialized, setAuthInitialized] = useState(false); // 변수명 변경
useEffect(() => {
const savedUser = localStorage.getItem('user');
if (savedUser) {
setCurrentUser(JSON.parse(savedUser));
}
setAuthInitialized(true); // 변수명 변경
}, []);
const login = async (userId, password) => {
try {
const response = await authApi.login({ userId, password });
const { accessToken } = response.data.data;
setAuthToken(accessToken);
const user = { userId };
setCurrentUser(user);
setAuthInitialized(true);
localStorage.setItem('user', JSON.stringify(user));
return true;
} catch (error) {
console.error('Login failed:', error);
return false;
}
};
const logout = async () => {
try {
if (currentUser) {
await authApi.logout({ userId: currentUser.userId });
}
} finally {
removeAuthToken();
setCurrentUser(null);
localStorage.removeItem('user');
}
};
return (
<AuthContext.Provider value={{ currentUser, login, logout, authInitialized }}>
{children}
</AuthContext.Provider>
);
};
export const useAuth = () => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};