Files
xrlms/src/app/login/page.tsx

332 lines
11 KiB
TypeScript
Raw Normal View History

2025-11-16 18:29:54 +09:00
"use client";
2025-11-25 12:38:11 +09:00
import { useState, useEffect } from "react";
2025-11-24 23:31:05 +09:00
import { useRouter } from "next/navigation";
2025-11-16 18:29:54 +09:00
import Link from "next/link";
2025-11-17 10:36:53 +09:00
import MainLogo from "@/app/svgs/mainlogosvg"
2025-11-18 01:37:56 +09:00
import LoginCheckboxActiveSvg from "@/app/svgs/logincheckboxactivesvg";
import LoginCheckboxInactiveSvg from "@/app/svgs/logincheckboxinactivesvg";
import LoginInputSvg from "@/app/svgs/inputformx";
import LoginErrorModal from "./LoginErrorModal";
2025-11-18 03:27:30 +09:00
import LoginOption from "@/app/login/LoginOption";
2025-11-16 18:29:54 +09:00
export default function LoginPage() {
2025-11-24 23:31:05 +09:00
const router = useRouter();
2025-11-16 18:29:54 +09:00
const [userId, setUserId] = useState("");
const [password, setPassword] = useState("");
const [rememberId, setRememberId] = useState(false);
const [autoLogin, setAutoLogin] = useState(false);
2025-11-18 01:37:56 +09:00
const [isUserIdFocused, setIsUserIdFocused] = useState(false);
const [isPasswordFocused, setIsPasswordFocused] = useState(false);
const [isLoginErrorOpen, setIsLoginErrorOpen] = useState(false);
2025-11-25 10:30:17 +09:00
const [idError, setIdError] = useState("");
const [passwordError, setPasswordError] = useState("");
2025-11-16 18:29:54 +09:00
// 컴포넌트 마운트 시 저장된 아이디 불러오기 및 자동 로그인 확인
2025-11-25 12:38:11 +09:00
useEffect(() => {
const savedId = localStorage.getItem('savedUserId');
if (savedId) {
setUserId(savedId);
setRememberId(true);
}
// 자동 로그인 확인: localStorage에 토큰이 있고 쿠키에도 토큰이 있으면 자동 로그인
const savedToken = localStorage.getItem('token');
const cookieToken = document.cookie
.split('; ')
.find(row => row.startsWith('token='))
?.split('=')[1];
if (savedToken && cookieToken && savedToken === cookieToken) {
// 토큰이 유효한지 확인
fetch('https://hrdi.coconutmeet.net/auth/me', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${savedToken}`,
},
})
.then(response => {
if (response.ok) {
// 토큰이 유효하면 메인 페이지로 리다이렉트
const searchParams = new URLSearchParams(window.location.search);
const redirectPath = searchParams.get('redirect') || '/';
router.push(redirectPath);
} else {
// 토큰이 유효하지 않으면 삭제
localStorage.removeItem('token');
document.cookie = 'token=; path=/; max-age=0';
}
})
.catch(() => {
// 에러 발생 시 토큰 삭제
localStorage.removeItem('token');
document.cookie = 'token=; path=/; max-age=0';
});
}
}, [router]);
2025-11-25 12:38:11 +09:00
// 아이디 기억하기 상태나 아이디가 변경될 때마다 저장 처리
useEffect(() => {
if (rememberId && userId.trim()) {
localStorage.setItem('savedUserId', userId);
} else if (!rememberId) {
localStorage.removeItem('savedUserId');
}
}, [rememberId, userId]);
2025-11-24 22:50:28 +09:00
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
2025-11-16 18:29:54 +09:00
e.preventDefault();
2025-11-25 12:38:11 +09:00
// 에러 초기화
setIdError("");
setPasswordError("");
// 입력 검증
let hasError = false;
if (userId.trim().length === 0) {
setIdError("아이디를 입력해 주세요.");
hasError = true;
}
if (password.trim().length === 0) {
setPasswordError("비밀번호를 입력해 주세요.");
hasError = true;
}
if (hasError) {
2025-11-24 22:50:28 +09:00
return;
}
try {
const response = await fetch("https://hrdi.coconutmeet.net/auth/login", {
method: "POST",
2025-11-25 10:30:17 +09:00
headers: { "Content-Type": "application/json", },
2025-11-24 22:50:28 +09:00
body: JSON.stringify({
email: userId,
password: password
})
});
if (!response.ok) {
let errorMessage = `로그인 실패 (${response.status})`;
try {
const errorData = await response.json();
if (errorData.error) {
errorMessage = errorData.error;
} else if (errorData.message) {
errorMessage = errorData.message;
} else if (response.statusText) {
errorMessage = `${response.statusText} (${response.status})`;
}
} catch (parseError) {
if (response.statusText) {
errorMessage = `${response.statusText} (${response.status})`;
}
}
console.error("로그인 실패:", errorMessage);
setIsLoginErrorOpen(true);
return;
}
const data = await response.json();
console.log("로그인 성공:", data);
2025-11-25 10:30:17 +09:00
2025-11-24 23:31:05 +09:00
// 로그인 성공 시 토큰 저장 (다양한 필드명 지원)
const token = data.token || data.accessToken || data.access_token;
if (token) {
if (autoLogin) {
// 자동 로그인이 체크되어 있으면 localStorage와 쿠키에 장기 저장 (30일)
localStorage.setItem('token', token);
document.cookie = `token=${token}; path=/; max-age=${30 * 24 * 60 * 60}; SameSite=Lax`;
console.log("자동 로그인 토큰 저장 완료 (30일 유지)");
} else {
// 자동 로그인이 체크되어 있지 않으면 쿠키에만 세션 쿠키로 저장 (브라우저 종료 시 삭제)
// localStorage에는 저장하지 않음
document.cookie = `token=${token}; path=/; SameSite=Lax`;
console.log("세션 토큰 저장 완료 (브라우저 종료 시 삭제)");
}
2025-11-24 23:31:05 +09:00
} else {
console.warn("토큰이 응답에 없습니다. 응답 데이터:", data);
// 토큰이 없어도 로그인은 성공했으므로 진행
}
2025-11-25 10:30:17 +09:00
2025-11-25 12:38:11 +09:00
2025-11-25 00:03:05 +09:00
// 리다이렉트 경로 확인
const searchParams = new URLSearchParams(window.location.search);
const redirectPath = searchParams.get('redirect') || '/';
2025-11-25 10:30:17 +09:00
2025-11-24 23:31:05 +09:00
// 메인 페이지로 이동
2025-11-25 00:03:05 +09:00
router.push(redirectPath);
2025-11-24 22:50:28 +09:00
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "네트워크 오류가 발생했습니다.";
console.error("로그인 오류:", errorMessage);
setIsLoginErrorOpen(true);
}
2025-11-16 18:29:54 +09:00
}
return (
2025-11-17 22:51:23 +09:00
<div className="min-h-screen w-full flex flex-col items-center justify-between">
2025-11-18 01:37:56 +09:00
<LoginErrorModal
open={isLoginErrorOpen}
onClose={() => setIsLoginErrorOpen(false)}
/>
2025-11-25 10:30:17 +09:00
<LoginOption
2025-11-18 01:37:56 +09:00
onClick={() => setIsLoginErrorOpen(true)}
2025-11-25 10:30:17 +09:00
loginErrorModalEnabled={isLoginErrorOpen}
setLoginErrorModalEnabled={setIsLoginErrorOpen}
2025-11-18 01:37:56 +09:00
/>
2025-11-17 22:51:23 +09:00
<div className="rounded-xl bg-white max-w-[560px] px-[40px] w-full">
2025-11-16 18:29:54 +09:00
{/* 로고 영역 */}
2025-11-18 01:37:56 +09:00
<div className="my-15 flex flex-col items-center">
2025-11-25 10:30:17 +09:00
<div className="mb-[7px]">
<MainLogo />
</div>
2025-11-17 22:51:23 +09:00
<div className="text-[28.8px] font-extrabold leading-[145%] text-neutral-700" >
2025-11-16 18:29:54 +09:00
XR LMS
</div>
</div>
{/* 폼 */}
2025-11-25 12:38:11 +09:00
<form onSubmit={handleSubmit} className="space-y-4">
2025-11-16 18:29:54 +09:00
<div className="space-y-4">
{/* 아이디 */}
2025-11-18 01:37:56 +09:00
<div className="relative">
2025-11-16 18:29:54 +09:00
<label htmlFor="userId" className="sr-only">
</label>
<input
id="userId"
name="userId"
value={userId}
2025-11-25 12:38:11 +09:00
onChange={(e) => {
setUserId(e.target.value);
if (idError) setIdError("");
}}
2025-11-18 01:37:56 +09:00
onFocus={() => setIsUserIdFocused(true)}
onBlur={() => setIsUserIdFocused(false)}
2025-11-25 12:38:11 +09:00
placeholder="아이디(이메일)"
className={`h-[56px] px-[12px] py-[7px] w-full rounded-[8px] border focus:outline-none focus:ring-0 focus:ring-offset-0 focus:shadow-none focus:appearance-none text-[18px] text-neutral-700 font-normal leading-[150%] placeholder:text-input-placeholder-text pr-[40px] ${idError ? 'border-error' : 'border-neutral-40 focus:border-neutral-700'}`}
2025-11-16 18:29:54 +09:00
/>
2025-11-18 01:37:56 +09:00
{userId.trim().length > 0 && isUserIdFocused && (
<button
type="button"
onMouseDown={(e) => {
e.preventDefault();
setUserId("");
}}
aria-label="입력 지우기"
className="absolute right-3 top-1/2 -translate-y-1/2 cursor-pointer"
>
<LoginInputSvg />
</button>
)}
2025-11-16 18:29:54 +09:00
</div>
2025-11-25 12:38:11 +09:00
{idError && <p className="text-error text-[13px] leading-tight mt-[10px]">{idError}</p>}
2025-11-16 18:29:54 +09:00
{/* 비밀번호 */}
2025-11-18 01:37:56 +09:00
<div className="relative">
2025-11-16 18:29:54 +09:00
<label htmlFor="password" className="sr-only">
</label>
<input
id="password"
name="password"
type="password"
value={password}
2025-11-25 12:38:11 +09:00
onChange={(e) => {
setPassword(e.target.value);
if (passwordError) setPasswordError("");
}}
2025-11-18 01:37:56 +09:00
onFocus={() => setIsPasswordFocused(true)}
onBlur={() => setIsPasswordFocused(false)}
2025-11-25 12:38:11 +09:00
placeholder="비밀번호 입력"
className={`h-[56px] px-[12px] py-[7px] rounded-[8px] w-full border focus:outline-none focus:ring-0 focus:ring-offset-0 focus:shadow-none focus:appearance-none text-[18px] text-neutral-700 font-normal leading-[150%] placeholder:text-input-placeholder-text pr-[40px] ${passwordError ? 'border-error' : 'border-neutral-40 focus:border-neutral-700'}`}
2025-11-16 18:29:54 +09:00
/>
2025-11-18 01:37:56 +09:00
{password.trim().length > 0 && isPasswordFocused && (
<button
type="button"
onMouseDown={(e) => {
e.preventDefault();
setPassword("");
}}
aria-label="입력 지우기"
className="absolute right-3 top-1/2 -translate-y-1/2 cursor-pointer"
>
<LoginInputSvg />
</button>
)}
2025-11-16 18:29:54 +09:00
</div>
2025-11-25 12:38:11 +09:00
{passwordError && <p className="text-error text-[13px] leading-tight mt-[4px]">{passwordError}</p>}
2025-11-16 18:29:54 +09:00
</div>
{/* 체크박스들 */}
2025-11-18 01:37:56 +09:00
<div className="flex items-center justify-start gap-6 mb-15">
2025-11-17 22:51:23 +09:00
<label className="flex cursor-pointer select-none items-center gap-2 text-[15px] font-normal text-basic-text">
2025-11-16 18:29:54 +09:00
<input
type="checkbox"
checked={rememberId}
onChange={(e) => setRememberId(e.target.checked)}
2025-11-18 01:37:56 +09:00
className="sr-only"
2025-11-16 18:29:54 +09:00
/>
2025-11-18 01:37:56 +09:00
{rememberId ? (
<LoginCheckboxActiveSvg />
) : (
<LoginCheckboxInactiveSvg />
)}
2025-11-16 18:29:54 +09:00
</label>
2025-11-17 22:51:23 +09:00
<label className="flex cursor-pointer select-none items-center gap-2 text-[15px] font-normal text-basic-text">
2025-11-16 18:29:54 +09:00
<input
type="checkbox"
checked={autoLogin}
onChange={(e) => setAutoLogin(e.target.checked)}
2025-11-18 01:37:56 +09:00
className="sr-only"
2025-11-16 18:29:54 +09:00
/>
2025-11-18 01:37:56 +09:00
{autoLogin ? (
<LoginCheckboxActiveSvg />
) : (
<LoginCheckboxInactiveSvg />
)}
2025-11-16 18:29:54 +09:00
</label>
</div>
{/* 로그인 버튼 */}
<button
type="submit"
2025-11-25 12:38:11 +09:00
className={`h-[56px] w-full rounded-lg text-[16px] font-semibold text-white transition-opacity cursor-pointer mb-3 hover:bg-[#1F2B91] ${userId.trim().length > 0 && password.trim().length > 0 ? "bg-active-button" : "bg-inactive-button"}`}
2025-11-16 18:29:54 +09:00
>
</button>
{/* 하단 링크들 */}
2025-11-18 01:37:56 +09:00
<div className="flex items-center justify-between text-[15px] leading-[150%] h-[36px]">
2025-11-16 18:29:54 +09:00
<Link
2025-11-17 22:51:23 +09:00
href="/register"
2025-11-18 01:37:56 +09:00
className="underline-offset-2 text-basic-text font-bold"
2025-11-16 18:29:54 +09:00
>
</Link>
<div
2025-11-17 22:51:23 +09:00
className="flex items-center gap-3 text-basic-text"
2025-11-16 18:29:54 +09:00
>
2025-11-18 03:27:30 +09:00
<Link href="/find-id" className="underline-offset-2">
2025-11-16 18:29:54 +09:00
</Link>
2025-11-17 22:51:23 +09:00
<span className="h-3 w-px bg-input-border" />
2025-11-18 03:27:30 +09:00
<Link href="/reset-password" className="underline-offset-2">
2025-11-16 18:29:54 +09:00
</Link>
</div>
</div>
</form>
</div>
2025-11-25 10:30:17 +09:00
<div></div>
2025-11-17 22:51:23 +09:00
<p className="text-center py-[40px] text-[15px] text-basic-text">
2025-11-16 18:29:54 +09:00
Copyright 2025 XL LMS. All rights reserved
</p>
</div>
);
}