I'm working on a Next.js 14 App Router project where I need to integrate two middlewares: Kinde authentication middleware (for protected routes like /dashboard). next-intl middleware (for internationalization on routes like /en or /de). Since Next.js only supports a single middleware export, I can't use two separate middlewares. Here's what my code looks like:
- next-intl middleware:
import createMiddleware from 'next-intl/middleware';
import {routing} from '@/i18n/routing';
export default createMiddleware(routing);
export const config = {
// Match only internationalized pathnames
matcher: ['/', '/(en|fr|ar)/:path*']
};
- kinde withAuth middleware:
import {
authMiddleware,
withAuth,
} from "@kinde-oss/kinde-auth-nextjs/middleware";
export default function middleware(req: Request) {
return withAuth(req);
}
export const config = {
matcher: ["/dashboard"],
};
I considered calling both middleware functions in the same file, but I'm not sure how to structure the logic correctly to handle both authentication and internationalization.