From 1b69dadf8689f435f8523edd42bc81af2492e32a Mon Sep 17 00:00:00 2001 From: kualta Date: Mon, 12 Aug 2024 15:20:40 +0300 Subject: [PATCH] fix middleware --- src/middleware.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index c77cd0b..3d31fa0 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -5,6 +5,7 @@ import { getCookieAuth } from "./utils/getCookieAuth"; export async function middleware(request: NextRequest) { const { pathname } = request.nextUrl; const { isValid: isAuthTokenValid } = getCookieAuth(); + console.log(isAuthTokenValid); // Check for the .lens postfix const lensNamespace = /^\/u\/(.+)\.lens$/; @@ -22,15 +23,15 @@ export async function middleware(request: NextRequest) { return NextResponse.redirect(new URL(`/u/${username}`, request.url)); } - // Define routes that are always accessible - const publicRoutes = ["/home", "/u", "/p"]; - - // Handle redirects - if (pathname === "/") { - // Always redirect the root path to /home + if (isAuthTokenValid && pathname === "/") { + // If authenticated redirect the root path to /home return NextResponse.redirect(new URL("/home", request.url)); } + // routes that are always accessible + const publicRoutes = ["/", "/home", "/u", "/p"]; + + if (!isAuthTokenValid) { // If not authenticated and trying to access a protected route, redirect to /home if (!publicRoutes.some((route) => pathname.startsWith(route))) { @@ -43,5 +44,5 @@ export async function middleware(request: NextRequest) { } export const config = { - matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"], + matcher: ["/((?!api|_next/static|_next/image|logo.png|home|favicon.ico).*)"], };