diff --git a/proxy.ts b/proxy.ts index 35d4006..807a5b9 100644 --- a/proxy.ts +++ b/proxy.ts @@ -1,9 +1,17 @@ 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) { - // Only checks that the cookie exists; getCurrentUser() still validates it. - if (!getSessionCookie(request)) { + 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)); } @@ -11,5 +19,5 @@ export function proxy(request: NextRequest) { } export const config = { - matcher: ["/((?!api|login|_next/static|_next/image|.*\\..*).*)"], + matcher: ["/((?!api|_next/static|_next/image|.*\\..*).*)"], };