- 페이지 폴더 재구성: safety/, attendance/ 폴더 신규 생성 - work/ → safety/: 이슈 신고, 출입 신청 관련 페이지 이동 - common/ → attendance/: 근태/휴가 관련 페이지 이동 - admin/ 정리: safety-* 파일들을 safety/로 이동 - 사이드바 네비게이션 메뉴 구현 - 카테고리별 메뉴: 작업관리, 안전관리, 근태관리, 시스템관리 - 접기/펼치기 기능 및 상태 저장 - 관리자 전용 메뉴 자동 표시/숨김 - 날씨 API 연동 (기상청 단기예보) - TBM 및 navbar에 현재 날씨 표시 - weatherService.js 추가 - 안전 체크리스트 확장 - 기본/날씨별/작업별 체크 유형 추가 - checklist-manage.html 페이지 추가 - 이슈 신고 시스템 구현 - workIssueController, workIssueModel, workIssueRoutes 추가 - DB 마이그레이션 파일 추가 (실행 대기) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
157 lines
4.7 KiB
JavaScript
157 lines
4.7 KiB
JavaScript
// /js/auth-check.js
|
|
// auth.js의 함수들을 직접 구현 (모듈 의존성 제거)
|
|
|
|
function isLoggedIn() {
|
|
const token = localStorage.getItem('token');
|
|
return token && token !== 'undefined' && token !== 'null';
|
|
}
|
|
|
|
function getUser() {
|
|
const user = localStorage.getItem('user');
|
|
return user ? JSON.parse(user) : null;
|
|
}
|
|
|
|
function clearAuthData() {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
localStorage.removeItem('userPageAccess'); // 페이지 권한 캐시도 삭제
|
|
}
|
|
|
|
/**
|
|
* 현재 페이지의 page_key를 URL 경로로부터 추출
|
|
* 예: /pages/work/tbm.html -> work.tbm
|
|
* /pages/admin/accounts.html -> admin.accounts
|
|
* /pages/dashboard.html -> dashboard
|
|
*/
|
|
function getCurrentPageKey() {
|
|
const path = window.location.pathname;
|
|
|
|
// /pages/로 시작하는지 확인
|
|
if (!path.startsWith('/pages/')) {
|
|
return null;
|
|
}
|
|
|
|
// /pages/ 이후 경로 추출
|
|
const pagePath = path.substring(7); // '/pages/' 제거
|
|
|
|
// .html 제거
|
|
const withoutExt = pagePath.replace('.html', '');
|
|
|
|
// 슬래시를 점으로 변환
|
|
const pageKey = withoutExt.replace(/\//g, '.');
|
|
|
|
return pageKey;
|
|
}
|
|
|
|
/**
|
|
* 사용자의 페이지 접근 권한 확인 (캐시 활용)
|
|
*/
|
|
async function checkPageAccess(pageKey) {
|
|
const currentUser = getUser();
|
|
|
|
// Admin은 모든 페이지 접근 가능
|
|
if (currentUser.role === 'Admin' || currentUser.role === 'System Admin') {
|
|
return true;
|
|
}
|
|
|
|
// 프로필 페이지는 모든 사용자 접근 가능
|
|
if (pageKey && pageKey.startsWith('profile.')) {
|
|
return true;
|
|
}
|
|
|
|
// 대시보드는 모든 사용자 접근 가능
|
|
if (pageKey === 'dashboard') {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
// 캐시된 권한 확인
|
|
const cached = localStorage.getItem('userPageAccess');
|
|
let accessiblePages = null;
|
|
|
|
if (cached) {
|
|
const cacheData = JSON.parse(cached);
|
|
// 캐시가 5분 이내인 경우 사용
|
|
if (Date.now() - cacheData.timestamp < 5 * 60 * 1000) {
|
|
accessiblePages = cacheData.pages;
|
|
}
|
|
}
|
|
|
|
// 캐시가 없으면 API 호출
|
|
if (!accessiblePages) {
|
|
const response = await fetch(`${window.API_BASE_URL}/users/${currentUser.user_id}/page-access`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.error('페이지 권한 조회 실패:', response.status);
|
|
return false;
|
|
}
|
|
|
|
const data = await response.json();
|
|
accessiblePages = data.data.pageAccess || [];
|
|
|
|
// 캐시 저장
|
|
localStorage.setItem('userPageAccess', JSON.stringify({
|
|
pages: accessiblePages,
|
|
timestamp: Date.now()
|
|
}));
|
|
}
|
|
|
|
// 해당 페이지에 대한 접근 권한 확인
|
|
const pageAccess = accessiblePages.find(p => p.page_key === pageKey);
|
|
return pageAccess && pageAccess.can_access === 1;
|
|
} catch (error) {
|
|
console.error('페이지 권한 체크 오류:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 즉시 실행 함수로 스코프를 보호하고 로직을 실행
|
|
(async function() {
|
|
if (!isLoggedIn()) {
|
|
console.log('🚨 인증되지 않은 사용자. 로그인 페이지로 이동합니다.');
|
|
clearAuthData(); // 만약을 위해 한번 더 정리
|
|
window.location.href = '/index.html';
|
|
return; // 이후 코드 실행 방지
|
|
}
|
|
|
|
const currentUser = getUser();
|
|
|
|
// 사용자 정보가 유효한지 확인 (토큰은 있지만 유저 정보가 깨졌을 경우)
|
|
if (!currentUser || !currentUser.username) {
|
|
console.error('🚨 사용자 정보가 유효하지 않습니다. 강제 로그아웃 처리합니다.');
|
|
clearAuthData();
|
|
window.location.href = '/index.html';
|
|
return;
|
|
}
|
|
|
|
const userRole = currentUser.role || currentUser.access_level || '사용자';
|
|
console.log(`✅ ${currentUser.username}(${userRole})님 인증 성공.`);
|
|
|
|
// 페이지 접근 권한 체크 (Admin은 건너뛰기)
|
|
if (currentUser.role !== 'Admin' && currentUser.role !== 'System Admin') {
|
|
const pageKey = getCurrentPageKey();
|
|
|
|
if (pageKey) {
|
|
console.log(`🔍 페이지 권한 체크: ${pageKey}`);
|
|
const hasAccess = await checkPageAccess(pageKey);
|
|
|
|
if (!hasAccess) {
|
|
console.error(`🚫 페이지 접근 권한이 없습니다: ${pageKey}`);
|
|
alert('이 페이지에 접근할 권한이 없습니다.');
|
|
window.location.href = '/pages/dashboard.html';
|
|
return;
|
|
}
|
|
|
|
console.log(`✅ 페이지 접근 권한 확인됨: ${pageKey}`);
|
|
}
|
|
}
|
|
|
|
// 역할 기반 메뉴 제어 로직은 각 컴포넌트 로더(load-navbar.js 등)로 이전함.
|
|
// 전역 변수 할당(window.currentUser) 제거.
|
|
})(); |