From ef4c533ebacc2cdc97e518f30e9a9350004fcdfb Mon Sep 17 00:00:00 2001 From: dujinkim Date: Mon, 28 Apr 2025 02:13:30 +0000 Subject: ~20250428 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'middleware.ts') diff --git a/middleware.ts b/middleware.ts index 84ee2313..88d30b2d 100644 --- a/middleware.ts +++ b/middleware.ts @@ -14,6 +14,8 @@ acceptLanguage.languages(languages); const publicPaths = [ '/evcp', '/partners', + '/partners/repository', + '/partners/signup', '/api/auth', ]; @@ -25,7 +27,7 @@ function isPublicPath(path: string, lng: string) { } // 2. auth API는 별도 처리 - if (path.startsWith('/api/auth')) { + if (path.includes('/api/auth')) { return true; } @@ -109,7 +111,7 @@ export async function middleware(request: NextRequest) { /** * 6. 인증된 사용자의 도메인-URL 일치 확인 및 리다이렉션 */ - if (token && token.domain) { + if (token && token.domain && !isPublicPath(pathname, detectedLng)) { // 사용자의 domain과 URL 경로가 일치하는지 확인 const redirectPath = getDomainRedirectPath(pathname, token.domain as string, detectedLng); @@ -121,6 +123,28 @@ export async function middleware(request: NextRequest) { } } + /** + * 6.5 이미 로그인한 사용자가 로그인 페이지에 접근할 경우 대시보드로 리다이렉트 + */ +if (token) { + // 로그인 페이지 경로 확인 (정확한 /ko/evcp 또는 /en/partners 등) + const isEvcpLoginPage = pathname === `/${detectedLng}/evcp`; + const isPartnersLoginPage = pathname === `/${detectedLng}/partners`; + + if (isEvcpLoginPage) { + // EVCP 로그인 페이지에 접근한 경우 report 페이지로 리다이렉트 + const redirectUrl = new URL(`/${detectedLng}/evcp/report`, origin); + redirectUrl.search = searchParams.toString(); + return NextResponse.redirect(redirectUrl); + } else if (isPartnersLoginPage) { + // Partners 로그인 페이지에 접근한 경우 dashboard 페이지로 리다이렉트 + const redirectUrl = new URL(`/${detectedLng}/partners/dashboard`, origin); + redirectUrl.search = searchParams.toString(); + return NextResponse.redirect(redirectUrl); + } +} + + /** * 7. 인증 확인: 공개 경로가 아닌 경우 로그인 체크 및 리다이렉트 */ @@ -138,7 +162,7 @@ export async function middleware(request: NextRequest) { // 로그인 후 원래 페이지로 리다이렉트하기 위해 callbackUrl 추가 const redirectUrl = new URL(loginPath, origin); - redirectUrl.searchParams.set('callbackUrl', request.url); + redirectUrl.searchParams.set('callbackUrl', request.nextUrl.pathname + request.nextUrl.search); return NextResponse.redirect(redirectUrl); } @@ -165,6 +189,6 @@ export async function middleware(request: NextRequest) { */ export const config = { matcher: [ - '/((?!_next|.*\\..*|api/(?!auth)).*)', + '/((?!_next|.*\\..*|api).*)', // API 경로 전체 제외 ], }; \ No newline at end of file -- cgit v1.2.3