Next.js Discord

Discord Forum

Next.js Middleware Not Running

Unanswered
Braconid wasp posted this in #help-forum
Open in Discord
Braconid waspOP
Problem: My Next.js middleware is not executing at all - no console logs, no redirects, protected pages are accessible.

Setup:

Next.js app deployed on Railway
Middleware file: middleware.js in project root
Goal: Redirect users to /login/verify-email if isEmailVerified cookie ≠ "true"

Current Code:

import { NextResponse } from 'next/server'

export function middleware(request) {
    console.log("Middleware running!", request.nextUrl.pathname)
    
    const isEmailVerified = request.cookies.get("isEmailVerified")?.value
    const pathname = request.nextUrl.pathname
    const allowedPaths = ["/", "/login/verify-email"]
    
    const isAllowed = allowedPaths.some(p => 
        pathname === p || pathname.startsWith(`${p}/`)
    )
    
    if (isEmailVerified !== "true" && !isAllowed) {
        return NextResponse.redirect(new URL("/login/verify-email", request.url))
    }
    
    return NextResponse.next()
}

export const config = {
    matcher: ['/:path*'],
}

1 Reply

Asian black bear
What's the location of your middleware file in relation to the src folder?