/** * 공유 네비게이션 헤더 * * 각 시스템 페이지에서 import하여 통합 포털 네비게이션을 제공 * */ (function() { const TOKEN_KEY = 'sso_token'; const USER_KEY = 'sso_user'; /** * SSO 토큰 가져오기 */ window.SSOAuth = { getToken: function() { return localStorage.getItem(TOKEN_KEY); }, getUser: function() { try { return JSON.parse(localStorage.getItem(USER_KEY)); } catch { return null; } }, isLoggedIn: function() { return !!this.getToken(); }, logout: function() { localStorage.removeItem(TOKEN_KEY); localStorage.removeItem(USER_KEY); localStorage.removeItem('sso_refresh_token'); window.location.href = '/login'; }, /** * 토큰을 Authorization 헤더에 포함한 fetch wrapper */ fetch: function(url, options) { options = options || {}; options.headers = options.headers || {}; const token = this.getToken(); if (token) { options.headers['Authorization'] = 'Bearer ' + token; } return fetch(url, options); }, /** * 토큰 유효성 확인 (SSO 서비스 호출) */ validate: async function() { const token = this.getToken(); if (!token) return false; try { const res = await fetch('/auth/validate', { headers: { 'Authorization': 'Bearer ' + token } }); return res.ok; } catch { return false; } }, /** * 로그인 안 되어있으면 로그인 페이지로 리다이렉트 */ requireLogin: function() { if (!this.isLoggedIn()) { window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname); } } }; })();