-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
37 lines (30 loc) · 986 Bytes
/
proxy.ts
File metadata and controls
37 lines (30 loc) · 986 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
import { NextRequest, NextResponse } from "next/server";
import { headers } from "next/headers";
import { auth } from "@/lib/auth";
const authRoutes = new Set([
"/sign-in",
"/sign-up",
"/forgot-password",
"/reset-password",
"/",
]);
export async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
const isAuthRoute = authRoutes.has(pathname);
const session = await auth.api.getSession({ headers: await headers() });
if (isAuthRoute) {
if (session)
return NextResponse.redirect(new URL("/dashboard", request.url));
return NextResponse.next();
}
//NOTE: only the dashboard routes are protected
if (!session && pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};