RTK Query + Redux + Next.js 15 Authentication Flow Issue (Session Hydration Delay) #4992
Unanswered
jsdev-robin
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm currently working with a Next.js 15 app using the App Router, and I'm handling authentication using RTK Query along with Redux Toolkit. My flow is:
After signing in, an accessToken is saved automatically in a browser cookie.
On subsequent page loads, I use a GET /me API request to retrieve the authenticated user.
Once the response arrives, I dispatch userSignin to update the authSlice with the session data (user, isAuth, and accessToken).
Everything works as expected, except for one critical issue:
This means:
Even though the user has a valid session cookie,
And the server returns the correct user via /me,
The page still redirects to /signin because Redux hasn't been updated in time.
I'm wondering if others have faced this session hydration timing issue with:
Next.js 15 (App Router)
Redux Toolkit + RTK Query
Cookie-based authentication
Client-side route protection (using authSlice state)
Looking for best practices to:
Delay route protection logic until the session is hydrated,
Or move session validation to middleware for early request-time decisions.
Any suggestions or links to relevant discussions are appreciated!
Beta Was this translation helpful? Give feedback.
All reactions