2026-03-13 23:18:01 +00:00
|
|
|
import { getSessionCookie } from "better-auth/cookies";
|
2025-09-21 11:02:31 -07:00
|
|
|
import { type NextRequest, NextResponse } from "next/server";
|
2024-03-14 20:00:52 +03:00
|
|
|
|
2026-03-13 23:18:01 +00:00
|
|
|
export function proxy(request: NextRequest) {
|
2025-04-25 23:40:15 -07:00
|
|
|
const { pathname } = request.nextUrl;
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2025-04-25 23:40:15 -07:00
|
|
|
/*
|
|
|
|
|
* Playwright starts the dev server and requires a 200 status to
|
|
|
|
|
* begin the tests, so this ensures that the tests can start
|
|
|
|
|
*/
|
2025-09-21 11:02:31 -07:00
|
|
|
if (pathname.startsWith("/ping")) {
|
|
|
|
|
return new Response("pong", { status: 200 });
|
2025-04-25 23:40:15 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (pathname.startsWith("/api/auth")) {
|
2025-04-25 23:40:15 -07:00
|
|
|
return NextResponse.next();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 23:18:01 +00:00
|
|
|
const sessionCookie = getSessionCookie(request);
|
2025-04-25 23:40:15 -07:00
|
|
|
|
2026-03-13 23:18:01 +00:00
|
|
|
if (sessionCookie && ["/login", "/register"].includes(pathname)) {
|
2026-02-23 08:14:17 -08:00
|
|
|
const url = request.nextUrl.clone();
|
|
|
|
|
url.pathname = "/";
|
|
|
|
|
return NextResponse.redirect(url);
|
2025-04-25 23:40:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.next();
|
|
|
|
|
}
|
2023-06-22 10:37:35 -07:00
|
|
|
|
|
|
|
|
export const config = {
|
2025-04-25 23:40:15 -07:00
|
|
|
matcher: [
|
2025-09-21 11:02:31 -07:00
|
|
|
"/",
|
|
|
|
|
"/chat/:id",
|
|
|
|
|
"/api/:path*",
|
|
|
|
|
"/login",
|
|
|
|
|
"/register",
|
2025-04-25 23:40:15 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Match all request paths except for the ones starting with:
|
|
|
|
|
* - _next/static (static files)
|
|
|
|
|
* - _next/image (image optimization files)
|
|
|
|
|
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
|
|
|
|
|
*/
|
2025-09-21 11:02:31 -07:00
|
|
|
"/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
2025-04-25 23:40:15 -07:00
|
|
|
],
|
2024-10-11 18:00:22 +05:30
|
|
|
};
|