-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
45 lines (40 loc) · 1.34 KB
/
proxy.ts
File metadata and controls
45 lines (40 loc) · 1.34 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
import { NextResponse } from "next/server";
import { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const accessToken = request.cookies.get("accessToken");
const refreshToken = request.cookies.get("refreshToken");
const notLogin = !accessToken && !refreshToken;
const notLoginDeniedPath = ["/generate", "/profile"];
// 로그인 유저 또는 이미 랜딩을 본 유저는 갤러리로 바로 이동
const visited = request.cookies.get("visited");
if (pathname === "/" && (accessToken || visited)) {
return NextResponse.redirect(new URL("/explore", request.url));
}
if (notLogin) {
if (notLoginDeniedPath.includes(pathname)) {
return NextResponse.redirect(new URL("/?needLogin=true", request.url));
}
}
if (
pathname.startsWith("/auth") &&
!pathname.includes("callback") &&
!notLogin
) {
return NextResponse.redirect(new URL("/explore", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public (public files)
*/
"/((?!api|_next/static|_next/image|favicon.ico|.*\\..*|public).*)",
],
};