Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
1 reply
niels

default NextAuth middleware and custom middleware conflicting

import { NextResponse, type NextRequest } from 'next/server';

export { default } from 'next-auth/middleware';

export const config = {
    matcher: [
        /*
         * Match all request paths except for the ones starting with:
         * - api (API routes)
         * - _next/static (static files)
         * - _next/image (image optimization files)
         * - favicon.ico (favicon file)
         * - login (login route)
         * - img (static images from the public folder)
         */
        '/((?!api|_next/static|_next/image|favicon.ico|login|img|logout).*)',
    ],
};

export function middleware(request: NextRequest) {
    if (request.nextUrl.pathname === '/') {
        return NextResponse.redirect(new URL('/products', request.url));
    }

    return NextResponse.next();
}


When I add this function middleware, the nextauth middleware stops working. I need the nextauth middleware to redirect to http://localhost:3000/login?callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fproducts when a user is not signed in.
Was this page helpful?