Chrome은 secure 쿠키 삭제 시 삭제 문자열에도 secure 플래그가 필요함. 6개 파일의 cookieRemove 함수에 '; secure; samesite=lax' 추가. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
229 lines
8.7 KiB
HTML
229 lines
8.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="ko">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>로그인 - TK 공장관리</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
background: #f0f2f5;
|
|
min-height: 100vh;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
.login-box {
|
|
background: white;
|
|
border-radius: 12px;
|
|
padding: 40px;
|
|
width: 100%;
|
|
max-width: 400px;
|
|
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
|
|
}
|
|
.login-box h1 {
|
|
text-align: center;
|
|
color: #1a56db;
|
|
font-size: 22px;
|
|
margin-bottom: 6px;
|
|
}
|
|
.login-box .sub {
|
|
text-align: center;
|
|
color: #6b7280;
|
|
font-size: 13px;
|
|
margin-bottom: 28px;
|
|
}
|
|
.form-group { margin-bottom: 16px; }
|
|
.form-group label {
|
|
display: block;
|
|
font-size: 13px;
|
|
font-weight: 500;
|
|
color: #374151;
|
|
margin-bottom: 6px;
|
|
}
|
|
.form-group input {
|
|
width: 100%;
|
|
padding: 10px 14px;
|
|
border: 1px solid #d1d5db;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
outline: none;
|
|
transition: border-color 0.2s;
|
|
}
|
|
.form-group input:focus {
|
|
border-color: #1a56db;
|
|
box-shadow: 0 0 0 3px rgba(26,86,219,0.1);
|
|
}
|
|
.btn-submit {
|
|
width: 100%;
|
|
padding: 12px;
|
|
background: #1a56db;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 8px;
|
|
font-size: 15px;
|
|
font-weight: 500;
|
|
cursor: pointer;
|
|
margin-top: 8px;
|
|
}
|
|
.btn-submit:hover { background: #1e40af; }
|
|
.btn-submit:disabled { background: #93c5fd; cursor: not-allowed; }
|
|
.error-msg {
|
|
color: #dc2626;
|
|
font-size: 13px;
|
|
text-align: center;
|
|
margin-top: 12px;
|
|
display: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="login-box">
|
|
<h1>TK 공장관리 시스템</h1>
|
|
<p class="sub">통합 로그인</p>
|
|
|
|
<form id="loginForm" onsubmit="handleLogin(event)">
|
|
<div class="form-group">
|
|
<label for="username">사용자명</label>
|
|
<input type="text" id="username" name="username" required autofocus autocomplete="username">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">비밀번호</label>
|
|
<input type="password" id="password" name="password" required autocomplete="current-password">
|
|
</div>
|
|
<button type="submit" class="btn-submit" id="submitBtn">로그인</button>
|
|
<p class="error-msg" id="errorMsg"></p>
|
|
</form>
|
|
</div>
|
|
|
|
<script>
|
|
// SSO 쿠키 유틸리티
|
|
var ssoCookie = {
|
|
set: function(name, value, days) {
|
|
var cookie = name + '=' + encodeURIComponent(value) + '; path=/';
|
|
if (days) cookie += '; max-age=' + (days * 86400);
|
|
if (window.location.hostname.includes('technicalkorea.net')) {
|
|
cookie += '; domain=.technicalkorea.net; secure; samesite=lax';
|
|
}
|
|
document.cookie = cookie;
|
|
},
|
|
get: function(name) {
|
|
var match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
|
|
return match ? decodeURIComponent(match[1]) : null;
|
|
},
|
|
remove: function(name) {
|
|
var cookie = name + '=; path=/; max-age=0';
|
|
if (window.location.hostname.includes('technicalkorea.net')) {
|
|
cookie += '; domain=.technicalkorea.net; secure; samesite=lax';
|
|
}
|
|
document.cookie = cookie;
|
|
}
|
|
};
|
|
|
|
async function handleLogin(e) {
|
|
e.preventDefault();
|
|
var btn = document.getElementById('submitBtn');
|
|
var errEl = document.getElementById('errorMsg');
|
|
errEl.style.display = 'none';
|
|
btn.disabled = true;
|
|
btn.textContent = '로그인 중...';
|
|
|
|
try {
|
|
var res = await fetch('/auth/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
username: document.getElementById('username').value,
|
|
password: document.getElementById('password').value
|
|
})
|
|
});
|
|
|
|
var data = await res.json();
|
|
|
|
if (!res.ok || !data.success) {
|
|
throw new Error(data.error || '로그인에 실패했습니다');
|
|
}
|
|
|
|
// 쿠키에 토큰 저장 (서브도메인 공유)
|
|
ssoCookie.set('sso_token', data.access_token, 7);
|
|
ssoCookie.set('sso_user', JSON.stringify(data.user), 7);
|
|
if (data.refresh_token) {
|
|
ssoCookie.set('sso_refresh_token', data.refresh_token, 30);
|
|
}
|
|
|
|
// localStorage에도 저장 (같은 도메인 내 호환성)
|
|
localStorage.setItem('sso_token', data.access_token);
|
|
localStorage.setItem('sso_user', JSON.stringify(data.user));
|
|
if (data.refresh_token) {
|
|
localStorage.setItem('sso_refresh_token', data.refresh_token);
|
|
}
|
|
|
|
// redirect 파라미터가 있으면 해당 URL로, 없으면 포털로
|
|
var redirect = new URLSearchParams(location.search).get('redirect');
|
|
if (redirect && isSafeRedirect(redirect)) {
|
|
window.location.href = redirect;
|
|
} else {
|
|
window.location.href = '/';
|
|
}
|
|
} catch (err) {
|
|
errEl.textContent = err.message;
|
|
errEl.style.display = '';
|
|
} finally {
|
|
btn.disabled = false;
|
|
btn.textContent = '로그인';
|
|
}
|
|
}
|
|
|
|
// 안전한 리다이렉트인지 확인 (같은 도메인 상대 경로 또는 *.technicalkorea.net)
|
|
function isSafeRedirect(url) {
|
|
if (!url) return false;
|
|
// 상대 경로
|
|
if (/^\/[a-zA-Z0-9]/.test(url) && !url.includes('://') && !url.includes('//')) {
|
|
return true;
|
|
}
|
|
// technicalkorea.net 서브도메인 절대 URL
|
|
try {
|
|
var parsed = new URL(url);
|
|
return parsed.hostname.endsWith('.technicalkorea.net') || parsed.hostname === 'technicalkorea.net';
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 토큰 만료 확인
|
|
function isTokenValid(token) {
|
|
try {
|
|
var payload = JSON.parse(atob(token.split('.')[1]));
|
|
return payload.exp > Math.floor(Date.now() / 1000);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 이미 로그인 되어있으면 포털로 (쿠키 또는 localStorage 체크 + 만료 확인)
|
|
var existingToken = ssoCookie.get('sso_token') || localStorage.getItem('sso_token');
|
|
if (existingToken && existingToken !== 'undefined' && existingToken !== 'null') {
|
|
if (isTokenValid(existingToken)) {
|
|
// 쿠키 재설정 (localStorage에만 있고 쿠키가 없는 경우 대비)
|
|
var existingUser = ssoCookie.get('sso_user') || localStorage.getItem('sso_user');
|
|
var existingRefresh = ssoCookie.get('sso_refresh_token') || localStorage.getItem('sso_refresh_token');
|
|
ssoCookie.set('sso_token', existingToken, 7);
|
|
if (existingUser) ssoCookie.set('sso_user', existingUser, 7);
|
|
if (existingRefresh) ssoCookie.set('sso_refresh_token', existingRefresh, 30);
|
|
|
|
var redirect = new URLSearchParams(location.search).get('redirect');
|
|
window.location.href = (redirect && isSafeRedirect(redirect)) ? redirect : '/';
|
|
} else {
|
|
// 만료된 토큰 정리
|
|
ssoCookie.remove('sso_token');
|
|
ssoCookie.remove('sso_user');
|
|
ssoCookie.remove('sso_refresh_token');
|
|
localStorage.removeItem('sso_token');
|
|
localStorage.removeItem('sso_user');
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|