I am using Next.js version 13.4.7 and have implemented a middleware that handles redirection based on a token. Now, I want to add a middleware for internationalization (i18n) using next-intl, but I am unsure how to chain multiple middlewares together in Next.js. Below is my code:
import { NextRequest, NextResponse } from 'next/server'
import jwt from 'jsonwebtoken'
import createMiddleware from 'next-intl/middleware'
const locales = ['en', 'fr']
const publicPages = ['/', '/signin', '/register']
const verifPages = ['/verifyaccount', '/resetpassword']
const i18nMiddleware = createMiddleware({
locales,
defaultLocale: 'en',
})
export const middleware = (request: NextRequest) => {
i18nMiddleware(request)
const path = request.nextUrl.pathname
const publicPathnameRegex = RegExp(
`^(/(${locales.join('|')}))?(${publicPages.join('|')})?/?$`,
'i'
)
const verifPathnameRegex = RegExp(
`^(/(${locales.join('|')}))?(${verifPages.join('|')})?/?$`,
'i'
)
const isPublicPage = publicPathnameRegex.test(path)
const isVerifPage = verifPathnameRegex.test(path)
const token = request.cookies.get('token')?.value || ''
const decodedToken: any = jwt.decode(token)
const headQuarter = decodedToken?.headQuarter
const redirectUrl = `/fr/hxoo/${headQuarter}/dashboard`
if (isPublicPage && token && !isVerifPage) {
return NextResponse.redirect(new URL(redirectUrl, request.nextUrl))
}
if (!isPublicPage && !token && !isVerifPage) {
return NextResponse.redirect(new URL('/signin', request.nextUrl))
}
if (!isPublicPage && token) {
const routeAccess = decodedToken?.routeAccess
const pathParts = path.split('/')
const route = `/${pathParts[3]}`
if (!routeAccess.includes(route)) {
return NextResponse.redirect(new URL(redirectUrl, request.nextUrl))
}
}
return NextResponse.next()
}
export const config = {
matcher: [
'/((?!api|_next|.*\\..*).*)',
],
}
I would appreciate any help on how to properly chain multiple middlewares in Next.js.