-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
23 lines (18 loc) · 777 Bytes
/
Copy pathproxy.ts
File metadata and controls
23 lines (18 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function proxy(request: NextRequest) {
const isProtectedRoute = request.nextUrl.pathname.startsWith("/chat");
// Check if session cookie exists
const sessionToken = request.cookies.get("next-auth.session-token") ||
request.cookies.get("__Secure-next-auth.session-token");
// Redirect unauthenticated users to sign-in page
if (isProtectedRoute && !sessionToken) {
const signInUrl = new URL("/auth/signin", request.url);
signInUrl.searchParams.set("callbackUrl", request.nextUrl.pathname);
return NextResponse.redirect(signInUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/chat/:path*"],
};