|
|
|
|
@ -1,17 +1,9 @@
|
|
|
|
|
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") {
|
|
|
|
|
// Only checks that the cookie exists; getCurrentUser() still validates it.
|
|
|
|
|
if (!getSessionCookie(request)) {
|
|
|
|
|
return NextResponse.redirect(new URL("/login", request.url));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -19,5 +11,5 @@ export function proxy(request: NextRequest) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
|
matcher: ["/((?!api|_next/static|_next/image|.*\\..*).*)"],
|
|
|
|
|
matcher: ["/((?!api|login|_next/static|_next/image|.*\\..*).*)"],
|
|
|
|
|
};
|
|
|
|
|
|