Skip to content

How to integrate next-intl middleware with an existing middleware #1613

Answered by AnouarMc
AssadRajab asked this question in Q&A
Discussion options

You must be logged in to vote

I think you can execute the next-intl middleware first. If the response is a redirect, return it otherwise, pass it to the auth middleware, and make sure to return that response if the auth middleware passes.

import createMiddleware from "next-intl/middleware";

const i18nMiddleware = createMiddleware(routing);

export default async function middleware(request: NextRequest) {
  const response = i18nMiddleware(request);

  if (response && !response.ok) {
    // response not in the range 200-299 (usually a redirect)
    // no need to execute the auth middleware
    return response;
  }
  return await authMiddleware(request, response);
}

async function authMiddleware(request: NextRequest, r…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@AssadRajab
Comment options

@AnouarMc
Comment options

@AssadRajab
Comment options

@AnouarMc
Comment options

Answer selected by AssadRajab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants