Redirect logged-in users from login page to home

main
Deardo Satria 2 days ago
parent 21d09f72b0
commit a99d371b39

@ -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|.*\\..*).*)"],
};

Loading…
Cancel
Save