API 재활용1
This commit is contained in:
@@ -9,6 +9,7 @@ import LoginCheckboxInactiveSvg from "@/app/svgs/logincheckboxinactivesvg";
|
||||
import LoginInputSvg from "@/app/svgs/inputformx";
|
||||
import LoginErrorModal from "./LoginErrorModal";
|
||||
import LoginOption from "@/app/login/LoginOption";
|
||||
import apiService from "@/app/lib/apiService";
|
||||
|
||||
export default function LoginPage() {
|
||||
const router = useRouter();
|
||||
@@ -40,45 +41,28 @@ export default function LoginPage() {
|
||||
|
||||
if (savedToken && cookieToken && savedToken === cookieToken) {
|
||||
// 토큰이 유효한지 확인
|
||||
const apiUrl = process.env.NEXT_PUBLIC_API_BASE_URL
|
||||
? `${process.env.NEXT_PUBLIC_API_BASE_URL}/auth/me`
|
||||
: 'https://hrdi.coconutmeet.net/auth/me';
|
||||
|
||||
fetch(apiUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${savedToken}`,
|
||||
},
|
||||
})
|
||||
apiService.getCurrentUser()
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
return response.json().then(userData => {
|
||||
// 계정 상태 확인
|
||||
const userStatus = userData.status || userData.userStatus;
|
||||
if (userStatus === 'INACTIVE' || userStatus === 'inactive') {
|
||||
// 비활성화된 계정인 경우 로그아웃 처리
|
||||
localStorage.removeItem('token');
|
||||
document.cookie = 'token=; path=/; max-age=0';
|
||||
return;
|
||||
}
|
||||
|
||||
// 사용자 권한 확인
|
||||
const userRole = userData.role || userData.userRole;
|
||||
if (userRole === 'ADMIN' || userRole === 'admin') {
|
||||
// admin 권한이면 /admin/id로 리다이렉트
|
||||
router.push('/admin/id');
|
||||
} else {
|
||||
// 그 외의 경우 기존 로직대로 리다이렉트
|
||||
const searchParams = new URLSearchParams(window.location.search);
|
||||
const redirectPath = searchParams.get('redirect') || '/';
|
||||
router.push(redirectPath);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 토큰이 유효하지 않으면 삭제
|
||||
const userData = response.data;
|
||||
// 계정 상태 확인
|
||||
const userStatus = userData.status || userData.userStatus;
|
||||
if (userStatus === 'INACTIVE' || userStatus === 'inactive') {
|
||||
// 비활성화된 계정인 경우 로그아웃 처리
|
||||
localStorage.removeItem('token');
|
||||
document.cookie = 'token=; path=/; max-age=0';
|
||||
return;
|
||||
}
|
||||
|
||||
// 사용자 권한 확인
|
||||
const userRole = userData.role || userData.userRole;
|
||||
if (userRole === 'ADMIN' || userRole === 'admin') {
|
||||
// admin 권한이면 /admin/id로 리다이렉트
|
||||
router.push('/admin/id');
|
||||
} else {
|
||||
// 그 외의 경우 기존 로직대로 리다이렉트
|
||||
const searchParams = new URLSearchParams(window.location.search);
|
||||
const redirectPath = searchParams.get('redirect') || '/';
|
||||
router.push(redirectPath);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -121,38 +105,8 @@ export default function LoginPage() {
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch("https://hrdi.coconutmeet.net/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", },
|
||||
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);
|
||||
setLoginErrorMessage(errorMessage);
|
||||
setIsLoginErrorOpen(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const response = await apiService.login(userId, password);
|
||||
const data = response.data;
|
||||
console.log("로그인 성공:", data);
|
||||
|
||||
// 로그인 성공 시 토큰 저장 (다양한 필드명 지원)
|
||||
@@ -176,23 +130,14 @@ export default function LoginPage() {
|
||||
|
||||
// 사용자 정보 가져오기 (권한 확인을 위해)
|
||||
try {
|
||||
const userResponse = await fetch('https://hrdi.coconutmeet.net/auth/me', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
const userResponse = await apiService.getCurrentUser();
|
||||
const userData = userResponse.data;
|
||||
const userRole = userData.role || userData.userRole;
|
||||
|
||||
if (userResponse.ok) {
|
||||
const userData = await userResponse.json();
|
||||
const userRole = userData.role || userData.userRole;
|
||||
|
||||
// admin 권한이면 /admin/id로 리다이렉트
|
||||
if (userRole === 'ADMIN' || userRole === 'admin') {
|
||||
router.push('/admin/id');
|
||||
return;
|
||||
}
|
||||
// admin 권한이면 /admin/id로 리다이렉트
|
||||
if (userRole === 'ADMIN' || userRole === 'admin') {
|
||||
router.push('/admin/id');
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("사용자 정보 조회 오류:", error);
|
||||
|
||||
Reference in New Issue
Block a user