forked from gorgeousTeam6/Schedo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (36 loc) · 1.16 KB
/
Copy pathmiddleware.ts
File metadata and controls
41 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
const token = request.cookies.get('token');
const { pathname } = request.nextUrl;
// 로그인이 되어있지 않고 보호된 페이지에 접근하려는 경우
if (!token) {
if (
pathname.startsWith('/dashboard') ||
pathname.startsWith('/mydashboard') ||
pathname.startsWith('/mypage')
) {
return NextResponse.redirect(new URL('/', request.url));
}
} else {
// 로그인이 되어있는데, 로그인 페이지나 메인 페이지에 접근하려는 경우
if (pathname === '/signin' || pathname === '/signup' || pathname === '/') {
return NextResponse.redirect(new URL('/mydashboard', request.url));
}
}
// 기본적으로 요청을 그대로 반환
return NextResponse.next();
}
// See "Matching Paths" below to learn more
export const config = {
matcher: [
'/dashboard/:path*',
'/mydashboard/:path*',
'/mypage/:path*',
'/signin',
'/signup',
'/',
],
};