You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using Next-Auth and Next-Intl together. I have progressed smoothly with the codes in the example section. My auth mechanizm works correctly. But in my project, I have some cases where I need to intercept with middleware before the requests I make hit the api routes. But i think with the current matcher configuration this becomes impossible.
Because if I remove the 'api' from the matcher configuration the requests always go with the locale prefix but my api folder has to be outside [locale]. I would like to ask for your support on this issue,
Thanks.
middleware.js
import{withAuth}from'next-auth/middleware'importcreateMiddlewarefrom'next-intl/middleware'import{locales}from'./lib/config/routes.js'constpublicPages=['/','/login','/register','/list','/cart','/about',]constintlMiddleware=createMiddleware({defaultLocale: 'tr',
locales,localePrefix: 'always',})constauthMiddleware=withAuth((req)=>intlMiddleware(req),{callbacks: {authorized: ({ token })=>token!=null,},pages: {signIn: '/login',},})exportdefaultasyncfunctionmiddleware(req,res){//!!!The requests I go to api routes do not hit middleware.constpathname=req.nextUrl.pathnameconstpublicPathnameRegex=RegExp(`^(/(${locales.join('|')}))?(${publicPages.flatMap((p)=>(p==='/' ? ['','/'] : p)).join('|')})/?$`,'i')constisPublicPage=publicPathnameRegex.test(pathname)if(isPublicPage){returnintlMiddleware(req)}else{returnauthMiddleware(req)}}exportconstconfig={matcher: ['/((?!api|_next|.*\\..*).*)','/(tr|en)/:path*',],}
My app structure:
The text was updated successfully, but these errors were encountered:
If you want to protect your Route Handlers with NextAuth, I guess you could change the matcher for them to pass also through the middleware, but add a condition within the middleware code to exclude them from running through the middleware from next-intl.
I'll move this to a discussion since it's a usage question.
Repository owner
locked and limited conversation to collaborators
May 19, 2024
Link to page
https://github.com/amannn/next-intl/tree/main/examples/example-app-router-next-auth
Describe the problem
I am using Next-Auth and Next-Intl together. I have progressed smoothly with the codes in the example section. My auth mechanizm works correctly. But in my project, I have some cases where I need to intercept with middleware before the requests I make hit the api routes. But i think with the current matcher configuration this becomes impossible.
Because if I remove the 'api' from the matcher configuration the requests always go with the locale prefix but my api folder has to be outside [locale]. I would like to ask for your support on this issue,
Thanks.
middleware.js
My app structure:
The text was updated successfully, but these errors were encountered: