"use client"; import { useEffect } from "react"; import { client } from "@/src/shared/api"; import type { IUserInfo } from "@/src/shared/store"; import { isAtLoginRedirectUrl } from "@common/network"; import { useUserStore, getCookie } from "@/src/shared/lib"; import { ENDPOINT, AUTH_USER_ID } from "@/src/shared/config"; const LOGIN_PAGE_URL = process.env.NEXT_PUBLIC_CM_LOGIN_PAGE_URL || "/auth/login"; const AuthWrapper = ({ children }: { children: React.ReactNode }) => { const { userInfo, setUserInfo } = useUserStore((state) => state); async function authCheck() { if (isAtLoginRedirectUrl(LOGIN_PAGE_URL)) { return; } const userId = getCookie(AUTH_USER_ID); if (!userId) { throw new Error("Missing required authentication tokens"); } const response = await client.get( `${ENDPOINT.USER_SERVICE.USERS}/info/me`, ); if (!userInfo) setUserInfo(response.data); } useEffect(() => { (async () => { try { await authCheck(); } catch (error) { console.error(error); setUserInfo(null); if (isAtLoginRedirectUrl(LOGIN_PAGE_URL)) { return; } // const hostname = window.location.hostname; // // 개발 환경에서는 localhost 도메인 사용 // if ( // hostname === "localhost" || // hostname === "127.0.0.1" || // hostname === "con.mind-one.co.kr" // ) { // router.push("/"); // 개발시 홈으로 돌아가기 // } // // 운영 환경에서는 서브도메인을 포함한 최상위 도메인 사용 // // router.push는 같은 Next.js 앱 내에서만 작동하므로 다른 앱으로 이동할 때는 window.location.href 사용 // if (hostname.includes("hasuon.or.kr")) // window.location.assign(LOGIN_PAGE_URL); // 세션 만료 시 CM 로그인 페이지로 리다이렉트 // 개발/운영 환경에서 로그인 URL 분기는 환경 변수로 처리 (.env.*) window.location.assign(LOGIN_PAGE_URL); } })(); }, []); return <>{children}; }; export { AuthWrapper };