Next.js Discord

Discord Forum

redirecting doesn't work properly in my middleware

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
code is in the gist: https://gist.github.com/xXnikosXx/09f8c2671e333d6ec62ccca3eff9f9b6
localeMiddleware.ts, authMiddleware.ts and stackHandler.ts are in /middlewares
middleware.ts and routes.ts are in /src

I'm trying to have protected pages in my nextjs project. I'm using next14 and auth.js v5

Ther localemiddleware, among other things, are supposed to add /en or /el to the url for localization.

the authMiddleware are supposed to check the current route based on routes.ts along with the login status of the user to properly redirect them

if the route is an auth api route, they should do nothing

if the route is an auth route (like /login or /signup) they are also supposed to do nothing IF the user is not logged in. if the user is logged in, they are supposed to redirect them to /settings

if the user isn't logged in and the route isn't a publicly accesible one (eg. settings, dashboard etc), they should be redirected to /login.

if none of the above are happening, again nothing should be done.

instead of following the rules above, when the user is not logged in and they are trying to access ANY route, they are redirected to /en//login, then to //login, then to /login, and so on so forth.
firefox stops all those redirections:

The page isn’t redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept cookies.

Anyone that has ruyn into this before or that could help with some pointers as to what tf im doing wrong?

0 Replies