46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { getSessionCookie } from "better-auth/cookies";
|
|
import { type NextRequest, NextResponse } from "next/server";
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
|
|
/*
|
|
* 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 });
|
|
}
|
|
|
|
if (pathname.startsWith("/api/auth")) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const sessionCookie = getSessionCookie(request);
|
|
|
|
if (sessionCookie && ["/login", "/register"].includes(pathname)) {
|
|
const url = request.nextUrl.clone();
|
|
url.pathname = "/";
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
"/",
|
|
"/chat/:id",
|
|
"/api/:path*",
|
|
"/login",
|
|
"/register",
|
|
|
|
/*
|
|
* 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).*)",
|
|
],
|
|
};
|