-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
66 lines (58 loc) · 1.92 KB
/
middleware.ts
File metadata and controls
66 lines (58 loc) · 1.92 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
import { NextRequest, NextResponse } from "next/server";
// 로그인 필요한 페이지
const PROTECTED_ROUTES = [
"/mypage",
"/connect/create",
"/connect/edit",
"/meetup/create",
"/favorites",
];
// 로그인한 유저가 접근하면 홈으로 리다이렉트
const AUTH_ROUTES = ["/login", "/signup", "/oauth"];
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const refreshToken = request.cookies.get("refreshToken")?.value;
// 로그인 한 유저가 진입시 -> 홈으로 리다이렉트
if (AUTH_ROUTES.some((route) => pathname.startsWith(route)) && refreshToken) {
return NextResponse.redirect(new URL("/", request.url));
}
// 인증되지 않은 유저가 인증이 필요한 URL로 진입시 -> 로그인페이지로이동
if (
(PROTECTED_ROUTES.some((route) => pathname.startsWith(route)) ||
(pathname.startsWith("/meetup/") &&
pathname
.split("/")
.filter((p) => p)
.pop() === "edit")) &&
!refreshToken
) {
return NextResponse.redirect(new URL("/login", request.url));
}
if (pathname.startsWith("/meetup/create")) {
const step = request.nextUrl.searchParams.get("step");
if (step !== null) {
const validStep = Number(step);
if (!Number.isInteger(validStep) || validStep < 1 || validStep > 4) {
const url = new URL("/meetup/create", request.url);
request.nextUrl.searchParams.forEach((value, key) => {
if (key !== "step") url.searchParams.set(key, value);
});
url.searchParams.set("step", "1");
return NextResponse.redirect(url);
}
}
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* 다음으로 시작하는 경로를 제외한 모든 요청 경로를 매칭합니다:
* - api (API 라우트)
* - _next/static (정적 파일)
* - _next/image (이미지 최적화 파일)
* - favicon.ico (파비콘 파일)
*/
"/((?!api|_next/static|_next/image|favicon.ico).*)",
],
};