Middleware
The Middleware (source code , Next.js docs ) is an execution layer on the backend that is executed just before a response is sent back to the client. It is mainly used for editing responses for redirection, authorization and some other stuff.
CurryBO uses middleware for
- displaying a page in the user’s preferred language, and
- forwarding to the login page if an unauthenticated user tries to visit any page except Main or Docs (or non-page routes).
The CurryBO middleware applies the following rules:
- If a user is logged in and trying to access the Login page, forward them to their callback or the main page if no callback was passed.
- Determine the desired locale
- if: the pathname has a locale, keep it
- else if: the referrer has a locale, keep it
- else: take the locale from the user agent
- If a user is not logged in and trying to access a protected route, forward them to the Login page
- If none of the above triggered, let the request pass with the correct locale.
The Middleware matches any route that follows this regex:
"/((?!api|docs|static|.*\\..*|_next|favicon.ico|robots.txt|serviceWorker.js).*)"Last updated on