@@ -10,6 +10,14 @@ import { useAuthStore } from '@/app/features/auth/store/useAuthStore'
1010// 로그인 없이 접근 가능한 경로
1111const PUBLIC_ROUTES = [ '/login' , '/signup' ]
1212
13+ // 보호 경로: 로그인 필요, 정규식 기반
14+ const PROTECTED_ROUTE_PATTERNS = [
15+ / ^ \/ d a s h b o a r d \/ [ ^ / ] + $ / , // /dashboard/:id
16+ / ^ \/ d a s h b o a r d \/ [ ^ / ] + \/ e d i t $ / , // /dashboard/:id/edit
17+ / ^ \/ m y p a g e $ / , // /mypage
18+ / ^ \/ m y d a s h b o a r d $ / , // /mydashboard
19+ ]
20+
1321export default function Redirect ( { children } : { children : React . ReactNode } ) {
1422 const router = useRouter ( )
1523 const pathname = usePathname ( )
@@ -20,33 +28,30 @@ export default function Redirect({ children }: { children: React.ReactNode }) {
2028
2129 const { data : firstDashboardId , isSuccess } = useFirstDashboardIdQuery ( )
2230
23- // ✅ 경로 파생값 선언 (중복 제거)
2431 const isRoot = pathname === '/'
2532 const isPublic = PUBLIC_ROUTES . includes ( pathname )
33+ const isProtectedRoute = PROTECTED_ROUTE_PATTERNS . some ( ( pattern ) =>
34+ pattern . test ( pathname ) ,
35+ )
2636
27- // 경로 변경 시 redirecting 상태 초기화
2837 useEffect ( ( ) => {
2938 if ( prevPath . current !== pathname ) {
3039 setRedirecting ( false )
3140 prevPath . current = pathname
3241 }
3342 } , [ pathname ] )
3443
35- // 로그인 상태와 경로에 따른 리다이렉트 처리
3644 useEffect ( ( ) => {
3745 if ( ! mounted || redirecting ) return
3846
39- // 1. 비로그인 + 루트(/): 랜딩 페이지 접근 허용
4047 if ( ! isLoggedIn && isRoot ) return
4148
42- // 2. 비로그인 + 보호 경로: 로그인 페이지로 이동
43- if ( ! isLoggedIn && ! isPublic && ! isRoot ) {
49+ if ( ! isLoggedIn && isProtectedRoute ) {
4450 setRedirecting ( true )
4551 router . replace ( '/login' )
4652 return
4753 }
4854
49- // 3. 로그인 + 루트(/): 대시보드 또는 마이대시보드로 이동
5055 if ( isLoggedIn && isRoot ) {
5156 if ( ! isSuccess ) return
5257 setRedirecting ( true )
@@ -56,14 +61,11 @@ export default function Redirect({ children }: { children: React.ReactNode }) {
5661 return
5762 }
5863
59- // 4. 로그인 + 퍼블릭 경로: 마이대시보드로 이동
6064 if ( isLoggedIn && isPublic ) {
6165 setRedirecting ( true )
6266 router . replace ( '/mydashboard' )
6367 return
6468 }
65-
66- // 5. 나머지는 접근 허용
6769 } , [
6870 pathname ,
6971 isLoggedIn ,
@@ -74,11 +76,10 @@ export default function Redirect({ children }: { children: React.ReactNode }) {
7476 firstDashboardId ,
7577 isRoot ,
7678 isPublic ,
79+ isProtectedRoute ,
7780 ] )
7881
79- // 🔒 깜빡임 방지: 루트 경로만 예외로 즉시 렌더링 허용
8082 if ( ! mounted && ! isRoot ) return null
8183
82- // ✅ 최종 렌더링
8384 return < > { children } </ >
8485}
0 commit comments