-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
78 lines (68 loc) · 2.3 KB
/
middleware.ts
File metadata and controls
78 lines (68 loc) · 2.3 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import parseJwt from "@/lib/parseJwt";
// 보호 경로 패턴
const protectedPaths = [
"/dashboard",
"/mypage",
"/vouchers",
"/my-vouchers",
"/notice",
"/notifications",
"/offline-stores",
"/payment",
"/wallet",
"/merchant/dashboard",
"/merchant/wallet",
"/merchant/notifications",
"/merchant/mypage"
];
export function middleware(request: NextRequest) {
const token = request.cookies.get("accessToken");
const pathname = request.nextUrl.pathname;
const isUserProtected = protectedPaths.some(
(path) => !path.startsWith("/merchant") && pathname.startsWith(path)
);
const isMerchantProtected = protectedPaths.some(
(path) => path.startsWith("/merchant") && pathname.startsWith(path)
);
// 보호 경로인데 토큰이 없으면 리다이렉트
if ((isUserProtected || isMerchantProtected) && !token) {
const redirectUrl = isMerchantProtected
? "/merchant/login"
: "/login";
return NextResponse.redirect(new URL(redirectUrl, request.url));
}
// 토큰이 존재하면 role까지 확인
if (token) {
const payload = parseJwt(token.value);
const role = payload?.role;
if (isUserProtected && role !== "USER") {
// 가맹점주가 일반 유저 보호 페이지 접근 시 → 일반 로그인 페이지로
return NextResponse.redirect(new URL("/login", request.url));
}
if (isMerchantProtected && role !== "MERCHANT") {
// 일반 유저가 가맹점 보호 페이지 접근 시 → 가맹점 로그인 페이지로
return NextResponse.redirect(new URL("/merchant/login", request.url));
}
}
return NextResponse.next();
}
// matcher 설정
export const config = {
matcher: [
"/dashboard/:path*",
"/mypage/:path*",
"/vouchers/:path*",
"/my-vouchers/:path*",
"/notice/:path*",
"/notifications/:path*",
"/offline-stores/:path*",
"/payment/:path*",
"/wallet/:path*",
"/merchant/dashboard/:path*",
"/merchant/wallet/:path*",
"/merchant/notifications/:path*",
"/merchant/mypage/:path*",
],
};