-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from jeiltodo/develop
Develop
- Loading branch information
Showing
19 changed files
with
152 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,47 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextRequest } from 'next/server'; | ||
|
||
// 관리자 보호 경로 목록 (basePath 적용) | ||
const protectedAdminRoutes = [ | ||
'/', | ||
'/goals', | ||
'/group', | ||
'/members', | ||
]; | ||
|
||
// 관리자 공개 경로 목록 (basePath 적용) | ||
const publicAdminRoutes = ['/login']; | ||
|
||
export function middleware(request: NextRequest) { | ||
const token = request.cookies.get('accessAdminToken')?.value; | ||
const path = request.nextUrl.pathname; | ||
|
||
console.log('path: ', path); | ||
console.log('관리자 Middleware running'); | ||
console.log('관리자 Middleware running'); | ||
console.log('Request URL:', request.nextUrl.pathname); | ||
console.log('Request URL:', path); | ||
console.log('Token:', token); | ||
if (request?.nextUrl.pathname === '/admin') { | ||
console.log('토큰있나요? ', token); | ||
|
||
// 보호된 관리자 경로에 접근 시 토큰 체크 | ||
if (protectedAdminRoutes.some(route => path === route || path.startsWith(`${route}/`))) { | ||
if (!token) { | ||
console.log('Redirecting to /login'); | ||
console.log('Redirecting to /admin/login'); | ||
return NextResponse.redirect(new URL('/admin/login', request.url)); | ||
} | ||
console.log('Token is present, proceeding...'); | ||
} | ||
if (request?.nextUrl.pathname === '/admin/login') { | ||
|
||
// 로그인한 관리자가 로그인 페이지에 접근하려 할 때 관리자 대시보드로 리다이렉트 | ||
if (publicAdminRoutes.some(route => path === route || path.startsWith(`${route}/`))) { | ||
if (token) { | ||
console.log('Redirecting to /'); | ||
return NextResponse.redirect(new URL('/admin', request.url)); | ||
console.log('Redirecting to /admin'); | ||
// return NextResponse.redirect(new URL('/admin', request.url)); | ||
} | ||
} | ||
|
||
// 다른 모든 경로에 대해서는 그대로 진행 | ||
return NextResponse.next(); | ||
} | ||
|
||
export const config = { | ||
matcher: ['/:path*'], | ||
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|logo.png|s-logo.png).*)'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,46 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextRequest } from 'next/server'; | ||
|
||
// 보호된 경로 목록 | ||
const protectedRoutes = ['/', '/goal', '/group', '/note', '/todo']; | ||
|
||
// 공개 경로 목록 (로그인하지 않아도 접근 가능한 경로) | ||
const publicRoutes = ['/login', '/signup']; | ||
|
||
export function middleware(request: NextRequest) { | ||
const token = request.cookies.get('accessToken')?.value; | ||
|
||
console.log('유저 Middleware running'); | ||
console.log('Request URL:', request.nextUrl.pathname); | ||
console.log('Token:', token); | ||
|
||
// 로그인을 하지 않아 토큰이 없으면 login페이지로 리다이렉트 | ||
if (request.nextUrl.pathname === '/') { | ||
const path = request.nextUrl.pathname; | ||
|
||
// 보호된 경로에 접근하려 할 때 토큰이 없으면 로그인 페이지로 리다이렉트 | ||
if ( | ||
protectedRoutes.some( | ||
(route) => path === route || path.startsWith(`${route}/`) | ||
) | ||
) { | ||
if (!token) { | ||
console.log('Redirecting to /login'); | ||
return NextResponse.redirect(new URL('/login', request.url)); | ||
} | ||
} | ||
|
||
//로그인을 해서 토큰이 있으면 login페이지로 못가도록 홈페이지로 리다이렉트 | ||
if (request.nextUrl.pathname === '/login') { | ||
// 이미 로그인한 사용자가 로그인 또는 회원가입 페이지에 접근하려 할 때 홈페이지로 리다이렉트 | ||
if (publicRoutes.includes(path)) { | ||
if (token) { | ||
console.log('Redirecting to /'); | ||
return NextResponse.redirect(new URL('/', request.url)); | ||
} | ||
} | ||
|
||
// 다른 모든 경로에 대해서는 그대로 진행 | ||
return NextResponse.next(); | ||
} | ||
|
||
//특정 path로만 해당 미들웨어가 동작 | ||
// 모든 경로에 대해 미들웨어 적용 | ||
export const config = { | ||
matcher: ['/:path*'], | ||
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.