-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
31 lines (24 loc) · 987 Bytes
/
middleware.ts
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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const hasRefreshToken = request.cookies.has("refreshToken");
const { pathname } = request.nextUrl;
const isAuthPage =
pathname.startsWith("/login") ||
pathname.startsWith("/signup") ||
pathname.startsWith("/reset") ||
pathname.startsWith("/oauth");
const isLandingPageOrAuthPage = pathname === "/" || isAuthPage;
if (hasRefreshToken && isAuthPage) {
return NextResponse.redirect(new URL("/", request.url));
}
// NOTE - 로그인 전에 랜딩, 로그인, 회원가입 페이지 외에 다른 페이지에 접근하는 경우
if (!hasRefreshToken && !isLandingPageOrAuthPage) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher:
"/((?!api|_next/static|_next/image|images|icons|lotties|fonts|favicon.ico).*)",
};