You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Authentication flow is not working normally with Redux Toolkit and RTK Query #4768
I'm using TanStack Router with Redux Toolkit and RTK Query, but I noticed that the authentication flow is not working as expected. When I try to log in for the first time, even if the username and password are correct, the redirection doesn't work. However, if I try again with the correct credentials, it works. The same issue happens on logout.
After a lot of testing, I noticed that if I use a regular reducer: {} inside createSlice to set the isAuth flag to true, the redirection works normally. But if I use extraReducers: {}, it doesn't work.
Example:
# using reducertosettheisAuthflagtheflowworksnormallyconstauthSliceBase=createSlice({name: 'auth',initialState: {isAuth: false},reducers: {setAuth: (status,{ payload }=>{state.isAuth =payload}}});
# using extraReducerswillnotworkingasexpectedconstauthSliceBase=createSlice({name: 'auth',initialState: {isAuth: false},reducers: {},builder.addMatcher(authApi.endpoints.login.matchFulfilled,(state,{ payload })=>{state.isAuth=true;}),builder.addMatcher(authApi.endpoints.logout.matchFulfilled,()=>initialState);});
Open the repro link.
Try to login using credentials given in the example
After first try to login is not redirecting to protected route
After try second time will work but should redirect first time not second time
Expected behavior
Authentication flow should work like normal flow login redirect to protected and logout redirect to login page automatically
I reproduced this on current main with the same RTK Query extraReducers flow.
This is the same context-invalidation issue already tracked in #2072. #6598 is a relevant draft attempt, but it is unmerged and its current test starts after router.update(), so it does not cover this React/Redux timing sequence.
Closing this as a duplicate of #2072 so the reproduction and fix work stay in one place.
Activity
bylly1 commentedon Aug 11, 2025
I also open a ticket to redux-toolkit and i received this message reduxjs/redux-toolkit#5041 (comment)
LadyBluenotes commentedon Jul 15, 2026
I reproduced this on current main with the same RTK Query
extraReducersflow.This is the same context-invalidation issue already tracked in #2072. #6598 is a relevant draft attempt, but it is unmerged and its current test starts after
router.update(), so it does not cover this React/Redux timing sequence.Closing this as a duplicate of #2072 so the reproduction and fix work stay in one place.