Skip to content

Commit

Permalink
fix middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Aug 12, 2024
1 parent e09a20f commit 1b69dad
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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$/;
Expand All @@ -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))) {
Expand All @@ -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).*)"],
};

0 comments on commit 1b69dad

Please sign in to comment.