-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
33 lines (25 loc) · 932 Bytes
/
Copy pathproxy.ts
File metadata and controls
33 lines (25 loc) · 932 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
import { NextRequest, NextResponse } from "next/server";
const protectedRoutes = ["/dashboard", "/settings"];
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// Only protect specific routes
const isProtected = protectedRoutes.some(
(route) => pathname === route || pathname.startsWith(route + "/"),
);
if (!isProtected) {
return NextResponse.next();
}
// Check for Better Auth session cookie
const sessionCookie =
request.cookies.get("better-auth.session_token") ??
request.cookies.get("__Secure-better-auth.session_token");
if (!sessionCookie?.value) {
const loginUrl = new URL("/", request.url);
return NextResponse.redirect(loginUrl);
}
// Cookie exists — allow through. Full session validation happens in API routes.
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/settings/:path*"],
};