Token Rotation Issue
Unanswered
Korat posted this in #help-forum
KoratOP
I am dealing with an issue while refreshing the token inside Route Handler.
Since cookies can't be modified from RSC or fetch wrappers my flow is like so:
1. In middleware I save a x-last-url inside a cookie every time a users goes to a page or even change queryParams.
2. Fetch wrapper catches 401 errors, in catch -> I call a route handler /api/refresh-token?token=new_session
3. In route handler i take the new token and i set a new token with that encrypted token.
4. After that I use NextResponse.redirect to redirect back to the lastUrl that user visited which is the current page.
Thing is that redirect sometime works sometimes not (mostly not), I see the correct url but the page is blank (its like some cache issues or I don't have an idea).
Screenshot provided:
Since cookies can't be modified from RSC or fetch wrappers my flow is like so:
1. In middleware I save a x-last-url inside a cookie every time a users goes to a page or even change queryParams.
2. Fetch wrapper catches 401 errors, in catch -> I call a route handler /api/refresh-token?token=new_session
3. In route handler i take the new token and i set a new token with that encrypted token.
4. After that I use NextResponse.redirect to redirect back to the lastUrl that user visited which is the current page.
Thing is that redirect sometime works sometimes not (mostly not), I see the correct url but the page is blank (its like some cache issues or I don't have an idea).
Screenshot provided:
2 Replies
KoratOP
Update: I was checking client logs, and what i noticed is that the moment of redirection, an infinite loop started with this message:
I commented all useEffects inside my app but the error still persists
Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.I commented all useEffects inside my app but the error still persists

KoratOP
Update: I think this has to do with NextResponse.redirect somehow caching that page=7 because when i try to move to page=8 its redirecting me still to page=7