How to stop middleware error page endless refresh?
Unanswered
Blanc de Hotot posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Blanc de HototOP
Hi
I have a middleware that validates the user's auth, checks if he has access role for that route, and either allows him to access the page or redirects him to the login.
My problem is that when the user is allowed to access a route ("./admin/.") but gets some error, like 404 page not found or something else, then his page refreshes endlessly.
A showcase of the middleware file and the video showcasing the endless refresh are attached.
Does anyone know what I can do to stop the page from refreshing endlessly. I have identified that the problem is with returning res, a TODO is set in the code.
Best regards Aron
I have a middleware that validates the user's auth, checks if he has access role for that route, and either allows him to access the page or redirects him to the login.
My problem is that when the user is allowed to access a route ("./admin/.") but gets some error, like 404 page not found or something else, then his page refreshes endlessly.
A showcase of the middleware file and the video showcasing the endless refresh are attached.
Does anyone know what I can do to stop the page from refreshing endlessly. I have identified that the problem is with returning res, a TODO is set in the code.
Best regards Aron
![Image](https://cdn.discordapp.com/attachments/1172874162277208164/1172874162591768676/Screenshot_2023-11-10_at_10.41.05.png?ex=6561e72f&is=654f722f&hm=8aa8684a0a68296fe042f08789497b895a51f981b31289610e14e9b47ef555b5&)
2 Replies
![Avatar](https://cdn.discordapp.com/avatars/678259999201427466/1936f7e8963ff0f800f5eb4b43bc833c.webp?size=256)
Clown
Haven't read that code in detail but i think the problem is that you arent checking if the destination of your redirect is the same as your current path.
Middleware runs before every request meaning its probably continuously redirecting you to login page because its failing the other checks, which in turn would re-run the middleware.
Middleware runs before every request meaning its probably continuously redirecting you to login page because its failing the other checks, which in turn would re-run the middleware.
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Blanc de HototOP
Hi, thank you for your reply.
Unfortunately, that is not the problem. When I compare req.nextUrl.pathname and req.url, they are always the same.
The problem is not with the login but rather when the user is allowed to access a page, but there is some next js error like page not found or application error, then the site all of a sudden goes into this crazy endless refresh, like demonstrated in the video :/
Unfortunately, that is not the problem. When I compare req.nextUrl.pathname and req.url, they are always the same.
The problem is not with the login but rather when the user is allowed to access a page, but there is some next js error like page not found or application error, then the site all of a sudden goes into this crazy endless refresh, like demonstrated in the video :/