Having problem setting up middleware in version 13
Unanswered
Param san posted this in #help-forum
I am using middleware in nextjs 13 app router and want to redirect the user's based on the auth. When I redirect users to the particular page lets say
/dashboard I am getting an error Too many redirects7 Replies
Show us some code ✨
There's nothing wrong with this piece of code, unless you're redirecting back to the home page from
/login or /dashboard. In that case you could have created a redirect loop@not-milo.tsx There's nothing wrong with this piece of code, **unless** you're redirecting back to the home page from `/login` or `/dashboard`. In that case you could have created a redirect loop
Yep, I have created a redirect loop, How can I solve that?
@Param san Yep, I have created a redirect loop, How can I solve that?
By breaking itðŸ§
@Param san Yep, I have created a redirect loop, How can I solve that?
Just break the redirect loop. Basically currently whenever you will go to
/login or /dashboard it will trigger the middleware and this will continue happening in a loop. You need to check if your current path isnt the path you are trying to redirect to. If it isnt you redirect, if it is you dont do anything.@Clown Just break the redirect loop. Basically currently whenever you will go to `/login` or `/dashboard` it will trigger the middleware and this will continue happening in a loop. You need to check if your current path isnt the path you are trying to redirect to. If it isnt you redirect, if it is you dont do anything.
Actually no, his matcher allows the middleware to run only on the homepage. Look at the matcher array.
The loop is created because the op added some form of redirection from either
Remove that redirecting piece of code and you'll solve your issue.
The loop is created because the op added some form of redirection from either
/login or /dashboard back to the home page.Remove that redirecting piece of code and you'll solve your issue.