-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.ts
More file actions
47 lines (39 loc) · 1013 Bytes
/
proxy.ts
File metadata and controls
47 lines (39 loc) · 1013 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { type NextRequest, NextResponse } from "next/server";
import { auth } from "@/lib/auth";
const publicRoutes = [
"/",
"/sign-in",
"/terms",
"/privacy",
"/api/auth",
"/api/webhook",
"/api/jobs",
"/api/calendar",
"/callback",
];
function isPublicRoute(pathname: string): boolean {
return publicRoutes.some(
(route) => pathname === route || pathname.startsWith(`${route}/`),
);
}
export async function proxy(req: NextRequest) {
const pathname = req.nextUrl.pathname;
if (isPublicRoute(pathname)) {
return NextResponse.next();
}
const session = await auth.api.getSession({
headers: req.headers,
});
if (!session) {
const signInUrl = new URL("/sign-in", req.url);
signInUrl.searchParams.set("redirect", pathname);
return NextResponse.redirect(signInUrl);
}
return NextResponse.next();
}
export const config = {
matcher: [
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
"/(api|trpc)(.*)",
],
};