Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
5 replies
Moussa

Nextjs middleware being ignored

Hey, this is driving me crazy, using next, why I'm not being redirected to "/pending" by the middleware when pushed to "/dashboard" from a different (login) route using useRouter push

login.tsx route

function goToDashboard(){
  router.push('/dashboard');
}
return <button onClick={goToDashboard}>go</button>


root middlware middleware.ts
export async function middleware(req: NextRequest) {
    const res = NextResponse.next();
    const { pathname } = req.nextUrl
    const status = "pending"
    const user = true
    if (pathname.startsWith("/dashboard")) {
        if (!user) {
            return redirect("/login", req);
        }
        if (status === "pending") {
            return redirect("/pending", req);
        }
    }
    return res
}

export const config = {
    matcher: [
        "/dashboard",
        "/login",
        "/pending",
    ],
}; 
Was this page helpful?