-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
31 lines (23 loc) · 913 Bytes
/
proxy.ts
File metadata and controls
31 lines (23 loc) · 913 Bytes
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";
import { AUTH_TOKEN_COOKIE } from "./lib/auth/constants";
const PUBLIC_ROUTES = new Set(["/login", "/callback"]);
function isPublicRoute(pathname: string): boolean {
return PUBLIC_ROUTES.has(pathname);
}
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = request.cookies.get(AUTH_TOKEN_COOKIE)?.value;
if (!token && !isPublicRoute(pathname)) {
const loginUrl = new URL("/login", request.url);
loginUrl.searchParams.set("next", pathname);
return NextResponse.redirect(loginUrl);
}
if (token && isPublicRoute(pathname) && pathname !== "/callback") {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|.*\\..*).*)"],
};