Next.js Discord

Discord Forum

Having problem setting up middleware in version 13

Unanswered
Param san posted this in #help-forum
Open in Discord
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 redirects

7 Replies

Show us some code ✨
@not-milo.tsx Show us some code ✨
Here you go.
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
@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 /login or /dashboard back to the home page.

Remove that redirecting piece of code and you'll solve your issue.