You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
755 B
TypeScript
24 lines
755 B
TypeScript
import { NextResponse, type NextRequest } from "next/server";
|
|
import { getSessionCookie } from "better-auth/cookies";
|
|
import { redirect } from "next/dist/server/api-utils";
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const isLoggedIn = !!getSessionCookie(request)
|
|
|
|
// if already logged in and accessing /login redirect to /
|
|
if (isLoggedIn && request.nextUrl.pathname === "/login") {
|
|
return NextResponse.redirect(new URL("/", request.url));
|
|
}
|
|
|
|
// If not logged in, redirect to /login
|
|
if (!isLoggedIn && request.nextUrl.pathname !== "/login") {
|
|
return NextResponse.redirect(new URL("/login", request.url));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!api|_next/static|_next/image|.*\\..*).*)"],
|
|
};
|