Next.js Discord

Discord Forum

Protected route by cookies in middleware not working with App router

Answered
Le Conte's Sparrow posted this in #help-forum
Open in Discord
Le Conte's SparrowOP
I have a few routes (RSC) need to be protected by cookies in a middleware.
The matter is the middleware seems to be called only once.
After I finish setCookie in login page and redirect back to protected routes by useRouter().replace('/protected') the middleware not triggered anymore. It just come back to login page.

I try to put a Link inside login page to click back to that route but it still doesn't work.
The only way I can do now is calling a refresh by window.location.reload(); right after setting cookie. But this seems a bit awkward.

Anyone can explain what happen and how to fix it?
Answered by Ray
sound like you hit router cache. try this
useRouter().refresh()
useRouter().replace('/protected')
View full answer

4 Replies