/** * 인증 관리 */ // 전역 변수로 설정 (중복 선언 방지) if (typeof window.currentUser === 'undefined') { window.currentUser = null; } // 페이지 로드 시 인증 상태 확인 (중복 실행 방지) if (!window.authInitialized) { window.authInitialized = true; document.addEventListener('DOMContentLoaded', () => { // dashboard.html에서는 자체적으로 인증 처리하므로 건너뜀 const isDashboardPage = window.location.pathname.endsWith('dashboard.html'); if (!isDashboardPage) { checkAuthStatus(); setupLoginForm(); } }); } // 인증 상태 확인 function checkAuthStatus() { const token = localStorage.getItem('authToken'); const userData = localStorage.getItem('currentUser'); // index.html에서는 토큰이 있으면 대시보드로 리다이렉트 (이미 위에서 처리됨) const isIndexPage = window.location.pathname.endsWith('index.html') || window.location.pathname === '/'; if (token && isIndexPage) { // 이미 index.html에서 리다이렉트 처리했으므로 여기서는 showAlreadyLoggedIn만 호출 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', showAlreadyLoggedIn); } else { showAlreadyLoggedIn(); } return; } if (token && !isIndexPage) { try { if (userData) { window.currentUser = JSON.parse(userData); } else { // 사용자 데이터가 없으면 기본값 사용 window.currentUser = { username: 'hyungi', full_name: 'Administrator' }; localStorage.setItem('currentUser', JSON.stringify(window.currentUser)); } showMainApp(); } catch (error) { console.error('사용자 데이터 파싱 실패:', error); // 파싱 실패 시 기본값으로 재시도 window.currentUser = { username: 'hyungi', full_name: 'Administrator' }; localStorage.setItem('currentUser', JSON.stringify(window.currentUser)); showMainApp(); } } else { showLoginScreen(); } } // 로그인 폼 설정 function setupLoginForm() { const loginForm = document.getElementById('loginForm'); if (loginForm) { loginForm.addEventListener('submit', handleLogin); } } // 로그인 처리 async function handleLogin(event) { event.preventDefault(); const username = document.getElementById('username').value; const password = document.getElementById('password').value; if (!username || !password) { alert('사용자명과 비밀번호를 입력해주세요.'); return; } try { showLoading(true); // 실제 API 호출 const response = await AuthAPI.login(username, password); window.currentUser = response.user; // 대시보드로 리다이렉트 window.location.href = 'dashboard.html'; } catch (error) { console.error('로그인 실패:', error); alert(error.message || '로그인에 실패했습니다.'); } finally { showLoading(false); } } // 로그아웃 function logout() { currentUser = null; localStorage.removeItem('authToken'); localStorage.removeItem('currentUser'); showLoginScreen(); } // 로그인 화면 표시 function showLoginScreen() { const loginScreen = document.getElementById('loginScreen'); const mainApp = document.getElementById('mainApp'); const alreadyLoggedIn = document.getElementById('alreadyLoggedIn'); if (loginScreen) { loginScreen.classList.remove('hidden'); } else { // dashboard.html에서는 로그인 페이지로 리다이렉트 window.location.href = 'index.html'; return; } if (mainApp) { mainApp.classList.add('hidden'); } if (alreadyLoggedIn) { alreadyLoggedIn.classList.add('hidden'); } // 폼 초기화 const loginForm = document.getElementById('loginForm'); if (loginForm) { loginForm.reset(); } } // 이미 로그인됨 표시 function showAlreadyLoggedIn() { const loginScreen = document.getElementById('loginScreen'); const alreadyLoggedIn = document.getElementById('alreadyLoggedIn'); if (loginScreen) { loginScreen.classList.remove('hidden'); } if (alreadyLoggedIn) { alreadyLoggedIn.classList.remove('hidden'); } // 로그인 폼 숨기기 const loginForm = document.getElementById('loginForm'); const testLoginSection = loginForm?.parentElement?.querySelector('.mt-4'); if (loginForm) { loginForm.style.display = 'none'; } if (testLoginSection) { testLoginSection.style.display = 'none'; } } // 메인 앱 표시 function showMainApp() { const loginScreen = document.getElementById('loginScreen'); const mainApp = document.getElementById('mainApp'); // index.html에서는 대시보드로 리다이렉트 if (!mainApp && loginScreen) { window.location.href = 'dashboard.html'; return; } if (loginScreen) { loginScreen.classList.add('hidden'); } if (mainApp) { mainApp.classList.remove('hidden'); } // 사용자 정보 표시 const currentUserElement = document.getElementById('currentUser'); if (currentUserElement && window.currentUser) { currentUserElement.textContent = window.currentUser.full_name || window.currentUser.username; } // Todo 목록 로드 if (typeof loadTodos === 'function') { loadTodos(); } } // 로딩 상태 표시 function showLoading(show) { const loadingOverlay = document.getElementById('loadingOverlay'); if (loadingOverlay) { if (show) { loadingOverlay.classList.remove('hidden'); } else { loadingOverlay.classList.add('hidden'); } } } // 전역으로 사용 가능하도록 export window.currentUser = currentUser; window.logout = logout; window.showLoading = showLoading;