fix: 로그인 후 사용자 역할 인식 문제 해결
Some checks failed
SonarQube Analysis / SonarQube Scan (push) Has been cancelled

- AuthContext를 통한 통합 인증 상태 관리로 변경
- App.jsx와 AuthContext 간 중복 사용자 상태 관리 제거
- SimpleLogin에서 AuthContext의 login 함수 직접 사용
- 로그인 후 사용자 역할이 즉시 올바르게 인식되도록 개선
- 새로고침 없이도 시스템 관리자 권한 정상 표시

변경사항:
- App.jsx: AuthProvider 래퍼 추가, 중복 인증 로직 제거
- SimpleLogin.jsx: AuthContext 직접 사용으로 변경
- 사용자 상태 동기화 문제 완전 해결
This commit is contained in:
Hyungi Ahn
2025-10-20 07:22:27 +09:00
parent ab607dfa9a
commit 0c99697a6f
6 changed files with 669 additions and 62 deletions

View File

@@ -0,0 +1,66 @@
{
"request_no": "PR-20251014-003",
"job_no": "테스트용",
"created_at": "2025-10-14T21:40:12.581699",
"materials": [
{
"material_id": 1,
"description": "HALF NIPPLE, SMLS, SCH 80S, ASTM A312 TP304 SW X NPT",
"category": "FITTING",
"size": "1/2\"",
"material_grade": "ASTM A312 TP304",
"quantity": 3,
"unit": "EA",
"user_requirement": ""
},
{
"material_id": 3,
"description": "HALF NIPPLE, SMLS, SCH 80S, ASTM A312 TP304 SW X NPT",
"category": "FITTING",
"size": "1/2\"",
"material_grade": "ASTM A312 TP304",
"quantity": 3,
"unit": "EA",
"user_requirement": ""
},
{
"material_id": 143,
"description": "HALF NIPPLE, SMLS, SCH 160, ASTM A106 B SW * NPT",
"category": "FITTING",
"size": "1\"",
"material_grade": "ASTM A106 B",
"quantity": 2,
"unit": "EA",
"user_requirement": ""
}
],
"grouped_materials": [
{
"group_key": "HALF NIPPLE, SMLS, SCH 80S, ASTM A312 TP304 SW X NPT|1/2\"|undefined|ASTM A312 TP304",
"material_ids": [
1,
3
],
"description": "HALF NIPPLE, SMLS, SCH 80S, ASTM A312 TP304 SW X NPT",
"category": "FITTING",
"size": "1/2\"",
"material_grade": "ASTM A312 TP304",
"quantity": 3,
"unit": "EA",
"user_requirement": ""
},
{
"group_key": "HALF NIPPLE, SMLS, SCH 160, ASTM A106 B SW * NPT|1\"|undefined|ASTM A106 B",
"material_ids": [
143
],
"description": "HALF NIPPLE, SMLS, SCH 160, ASTM A106 B SW * NPT",
"category": "FITTING",
"size": "1\"",
"material_grade": "ASTM A106 B",
"quantity": 2,
"unit": "EA",
"user_requirement": ""
}
]
}

Binary file not shown.

View File

@@ -0,0 +1,576 @@
{
"request_no": "PR-20251014-004",
"job_no": "테스트용",
"created_at": "2025-10-14T21:44:18.401404",
"materials": [
{
"material_id": 4,
"description": "PIPE, SMLS, SCH 40S, ASTM A312 TP304",
"category": "PIPE",
"size": "1/2\"",
"material_grade": "ASTM A312 TP304",
"quantity": 11,
"unit": "EA",
"user_requirement": ""
},
{
"material_id": 12,
"description": "PIPE, SMLS, SCH 80, ASTM A106 B",
"category": "PIPE",
"size": "3/4\"",
"material_grade": "ASTM A106 B",
"quantity": 92,
"unit": "EA",
"user_requirement": ""
}
],
"grouped_materials": [
{
"group_key": "PIPE, SMLS, SCH 40S, ASTM A312 TP304|1/2\"|undefined|ASTM A312 TP304",
"material_ids": [
4
],
"description": "PIPE, SMLS, SCH 40S, ASTM A312 TP304",
"category": "PIPE",
"size": "1/2\"",
"material_grade": "ASTM A312 TP304",
"quantity": 11,
"unit": "m",
"total_length": 1395.1,
"pipe_lengths": [
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 155,
"quantity": 1,
"totalLength": 155
},
{
"length": 155,
"quantity": 1,
"totalLength": 155
},
{
"length": 200,
"quantity": 1,
"totalLength": 200
},
{
"length": 245.1,
"quantity": 1,
"totalLength": 245.1
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
}
],
"user_requirement": ""
},
{
"group_key": "PIPE, SMLS, SCH 80, ASTM A106 B|3/4\"|undefined|ASTM A106 B",
"material_ids": [
12
],
"description": "PIPE, SMLS, SCH 80, ASTM A106 B",
"category": "PIPE",
"size": "3/4\"",
"material_grade": "ASTM A106 B",
"quantity": 92,
"unit": "m",
"total_length": 7920.2,
"pipe_lengths": [
{
"length": 60,
"quantity": 1,
"totalLength": 60
},
{
"length": 60,
"quantity": 1,
"totalLength": 60
},
{
"length": 60,
"quantity": 1,
"totalLength": 60
},
{
"length": 60,
"quantity": 1,
"totalLength": 60
},
{
"length": 43.3,
"quantity": 1,
"totalLength": 43.3
},
{
"length": 43.3,
"quantity": 1,
"totalLength": 43.3
},
{
"length": 43.3,
"quantity": 1,
"totalLength": 43.3
},
{
"length": 43.3,
"quantity": 1,
"totalLength": 43.3
},
{
"length": 43.3,
"quantity": 1,
"totalLength": 43.3
},
{
"length": 43.3,
"quantity": 1,
"totalLength": 43.3
},
{
"length": 50,
"quantity": 1,
"totalLength": 50
},
{
"length": 50,
"quantity": 1,
"totalLength": 50
},
{
"length": 50,
"quantity": 1,
"totalLength": 50
},
{
"length": 50,
"quantity": 1,
"totalLength": 50
},
{
"length": 50,
"quantity": 1,
"totalLength": 50
},
{
"length": 50,
"quantity": 1,
"totalLength": 50
},
{
"length": 50,
"quantity": 1,
"totalLength": 50
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 70,
"quantity": 1,
"totalLength": 70
},
{
"length": 76.2,
"quantity": 1,
"totalLength": 76.2
},
{
"length": 76.2,
"quantity": 1,
"totalLength": 76.2
},
{
"length": 76.2,
"quantity": 1,
"totalLength": 76.2
},
{
"length": 76.2,
"quantity": 1,
"totalLength": 76.2
},
{
"length": 76.2,
"quantity": 1,
"totalLength": 76.2
},
{
"length": 76.2,
"quantity": 1,
"totalLength": 76.2
},
{
"length": 77.6,
"quantity": 1,
"totalLength": 77.6
},
{
"length": 77.6,
"quantity": 1,
"totalLength": 77.6
},
{
"length": 77.6,
"quantity": 1,
"totalLength": 77.6
},
{
"length": 77.6,
"quantity": 1,
"totalLength": 77.6
},
{
"length": 77.6,
"quantity": 1,
"totalLength": 77.6
},
{
"length": 77.6,
"quantity": 1,
"totalLength": 77.6
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 80,
"quantity": 1,
"totalLength": 80
},
{
"length": 88.6,
"quantity": 1,
"totalLength": 88.6
},
{
"length": 88.6,
"quantity": 1,
"totalLength": 88.6
},
{
"length": 98.4,
"quantity": 1,
"totalLength": 98.4
},
{
"length": 98.4,
"quantity": 1,
"totalLength": 98.4
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 100,
"quantity": 1,
"totalLength": 100
},
{
"length": 120,
"quantity": 1,
"totalLength": 120
},
{
"length": 120,
"quantity": 1,
"totalLength": 120
},
{
"length": 150,
"quantity": 1,
"totalLength": 150
},
{
"length": 150,
"quantity": 1,
"totalLength": 150
},
{
"length": 150,
"quantity": 1,
"totalLength": 150
},
{
"length": 150,
"quantity": 1,
"totalLength": 150
},
{
"length": 150,
"quantity": 1,
"totalLength": 150
},
{
"length": 223.6,
"quantity": 1,
"totalLength": 223.6
}
],
"user_requirement": ""
}
]
}

Binary file not shown.

View File

@@ -13,15 +13,14 @@ import PurchaseRequestPage from './pages/PurchaseRequestPage';
import SystemLogsPage from './pages/SystemLogsPage'; import SystemLogsPage from './pages/SystemLogsPage';
import LogMonitoringPage from './pages/LogMonitoringPage'; import LogMonitoringPage from './pages/LogMonitoringPage';
import InactiveProjectsPage from './pages/InactiveProjectsPage'; import InactiveProjectsPage from './pages/InactiveProjectsPage';
import { AuthProvider, useAuth } from './contexts/AuthContext';
import errorLogger from './utils/errorLogger'; import errorLogger from './utils/errorLogger';
import api from './api'; import api from './api';
import './App.css'; import './App.css';
function App() { function AppContent() {
// TK-MP BOM Management System v2.0 - Project Selection Dashboard // TK-MP BOM Management System v2.0 - Project Selection Dashboard
const [isAuthenticated, setIsAuthenticated] = useState(false); const { user, isAuthenticated, isLoading, logout } = useAuth();
const [isLoading, setIsLoading] = useState(true);
const [user, setUser] = useState(null);
const [currentPage, setCurrentPage] = useState('dashboard'); const [currentPage, setCurrentPage] = useState('dashboard');
const [pageParams, setPageParams] = useState({}); const [pageParams, setPageParams] = useState({});
const [selectedProject, setSelectedProject] = useState(null); const [selectedProject, setSelectedProject] = useState(null);
@@ -177,30 +176,13 @@ function App() {
}); });
}; };
// 인증 상태 확인 // 인증된 사용자의 데이터 로드
useEffect(() => { useEffect(() => {
const checkAuth = async () => { if (isAuthenticated && user) {
try { loadPendingSignups();
const token = localStorage.getItem('access_token'); loadProjects();
if (token) { }
api.defaults.headers.common['Authorization'] = `Bearer ${token}`; }, [isAuthenticated, user]);
const userResponse = await api.get('/auth/me');
setUser(userResponse.data.user);
setIsAuthenticated(true);
await loadPendingSignups();
await loadProjects(); // 프로젝트 로드 추가
}
} catch (error) {
console.error('인증 확인 실패:', error);
localStorage.removeItem('access_token');
localStorage.removeItem('user_data');
setIsAuthenticated(false);
} finally {
setIsLoading(false);
}
};
checkAuth();
}, []);
// 페이지 이동 함수 // 페이지 이동 함수
const navigateToPage = (page, params = {}) => { const navigateToPage = (page, params = {}) => {
@@ -359,22 +341,11 @@ function App() {
} }
}; };
const handleLogout = () => { const handleLogout = async () => {
localStorage.removeItem('access_token'); await logout();
localStorage.removeItem('user_data');
setIsAuthenticated(false);
setUser(null);
setCurrentPage('dashboard'); setCurrentPage('dashboard');
window.location.reload();
}; };
const handleLoginSuccess = (userData) => {
setIsAuthenticated(true);
setUser(userData);
setIsLoading(false);
loadPendingSignups();
loadProjects();
};
return ( return (
<ErrorBoundary errorContext={{ user, currentPage, pageParams }}> <ErrorBoundary errorContext={{ user, currentPage, pageParams }}>
@@ -392,7 +363,7 @@ function App() {
</div> </div>
</div> </div>
) : !isAuthenticated ? ( ) : !isAuthenticated ? (
<SimpleLogin onLoginSuccess={handleLoginSuccess} /> <SimpleLogin />
) : ( ) : (
<div style={{ minHeight: '100vh', background: '#f7fafc' }}> <div style={{ minHeight: '100vh', background: '#f7fafc' }}>
{/* 상단 헤더 */} {/* 상단 헤더 */}
@@ -429,4 +400,13 @@ function App() {
); );
} }
// AuthProvider로 감싸는 래퍼 컴포넌트
function App() {
return (
<AuthProvider>
<AppContent />
</AuthProvider>
);
}
export default App; export default App;

View File

@@ -1,7 +1,9 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { useAuth } from './contexts/AuthContext';
import api from './api'; import api from './api';
const SimpleLogin = ({ onLoginSuccess }) => { const SimpleLogin = () => {
const { login } = useAuth();
const [formData, setFormData] = useState({ const [formData, setFormData] = useState({
username: '', username: '',
password: '' password: ''
@@ -110,28 +112,11 @@ const SimpleLogin = ({ onLoginSuccess }) => {
setError(''); setError('');
try { try {
const response = await api.post('/auth/login', formData); await login(formData.username, formData.password);
const data = response.data; setSuccess('로그인 성공! 대시보드로 이동합니다...');
if (data.success) {
// 토큰과 사용자 정보 저장
localStorage.setItem('access_token', data.access_token);
localStorage.setItem('user_data', JSON.stringify(data.user));
setSuccess('로그인 성공! 대시보드로 이동합니다...');
// 잠깐 성공 메시지 보여준 후 대시보드로 이동
setTimeout(() => {
if (onLoginSuccess) {
onLoginSuccess();
}
}, 1000);
} else {
setError(data.error?.message || '로그인에 실패했습니다.');
}
} catch (err) { } catch (err) {
console.error('Login error:', err); console.error('Login error:', err);
setError(err.response?.data?.message || '서버 연결에 실패했습니다.'); setError(err.message || '서버 연결에 실패했습니다.');
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }