Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs]: Middleware matcher with Next-Auth #1074

Closed
hakan-berat opened this issue May 18, 2024 · 1 comment
Closed

[Docs]: Middleware matcher with Next-Auth #1074

hakan-berat opened this issue May 18, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.

Comments

@hakan-berat
Copy link

hakan-berat commented May 18, 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

import { withAuth } from 'next-auth/middleware'
import createMiddleware from 'next-intl/middleware'
import { locales } from './lib/config/routes.js'

const publicPages = [
  '/',
  '/login',
  '/register',
  '/list',
  '/cart',
  '/about',
]

const intlMiddleware = createMiddleware({
  defaultLocale: 'tr',
  locales,
  localePrefix: 'always',
})

const authMiddleware = withAuth((req) => intlMiddleware(req), {
  callbacks: {
    authorized: ({ token }) => token != null,
  },
  pages: {
    signIn: '/login',
  },
})

export default async function middleware(req, res) {
  //!!!The requests I go to api routes do not hit middleware.
  const pathname = req.nextUrl.pathname
  const publicPathnameRegex = RegExp(
    `^(/(${locales.join('|')}))?(${publicPages
      .flatMap((p) => (p === '/' ? ['', '/'] : p))
      .join('|')})/?$`,
    'i'
  )
  const isPublicPage = publicPathnameRegex.test(pathname)

  if (isPublicPage) {
    return intlMiddleware(req)
  } else {
    return authMiddleware(req)
  }
}

export const config = {
  matcher: [
    '/((?!api|_next|.*\\..*).*)',
    '/(tr|en)/:path*',
  ],
}

My app structure:

image

@hakan-berat hakan-berat added documentation Improvements or additions to documentation unconfirmed Needs triage. labels May 18, 2024
@amannn
Copy link
Owner

amannn commented May 19, 2024

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
@amannn amannn converted this issue into discussion #1076 May 19, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
documentation Improvements or additions to documentation unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

2 participants