chatbot-template/proxy.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import { getSessionCookie } from "better-auth/cookies";
import { type NextRequest, NextResponse } from "next/server";
2024-03-14 20:00:52 +03: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
*/
if (pathname.startsWith("/ping")) {
return new Response("pong", { status: 200 });
2025-04-25 23:40:15 -07:00
}
if (pathname.startsWith("/api/auth")) {
2025-04-25 23:40:15 -07:00
return NextResponse.next();
}
const sessionCookie = getSessionCookie(request);
2025-04-25 23:40:15 -07:00
if (sessionCookie && ["/login", "/register"].includes(pathname)) {
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: [
"/",
"/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)
*/
"/((?!_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
};