I'm facing a problem with middleware when using NextResponse.next() method in the Next.js 14 app router, here is a code snippet about the code that i'm using in the middleware
if (request.nextUrl.pathname.startsWith("/dashboard")) {
if (!accessToken && !refreshToken) {
return NextResponse.redirect(new URL("/adminLogin", request.url))
}
return NextResponse.next();
}
I'm trying to continue to the next route handler by using NextResponse.next() and i expected to be redirected to the dashboad page
I found the solution and the problem cause, the middleware that I'm using in my code is like
export default async function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith("/dashboard")) {
if (!accessToken && !refreshToken) {
return NextResponse.redirect(new URL("/adminLogin", request.url))
}
return NextResponse.next();
}
return i18nMiddleware(request);
}
and the file structure is like
├- src/
| ├- app/
| ├- [locale]/
| ├- dashboard/
|
├ middleware.ts
So the real problem is when the user is authenticated or has a refresh token I'm returning NextResponse.next() which stops the middleware without launching the i18nMiddleware.
I don't know how i18nMiddleware deals with the default locale but I think it changes the path from https://domainName/en/dashboard to https://domainName/dashboard after rendering so no errors happen because the request is sent to the right path which is /en/dashboard