Skip to content

Authentication flow is not working normally with Redux Toolkit and RTK Query #4768

Closed as duplicate

Description

@bylly1

Which project does this relate to?

Router

Describe the bug

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 reducer to set the isAuth flag the flow works normally

const authSliceBase = createSlice({
  name: 'auth',
  initialState: {
     isAuth: false
  },
  reducers: {
     setAuth: (status, { payload } => {
         state.isAuth = payload
     }
  }
});

# using extraReducers will not working as expected

const authSliceBase = 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
      );
});

Your Example Website or App

https://stackblitz.com/edit/vitejs-vite-kumuejx2

Steps to Reproduce the Bug or Issue

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

Screenshots or Videos

No response

Platform

  • Router / Start Version: ^1.129.4
  • OS: Windows
  • Browser: Chrome
  • Browser Version: 138
  • Bundler: vite
  • Bundler Version: 7.0.4

Additional context

No response

Activity

  1. bylly1 commented on Aug 11, 2025

    @bylly1
    Author

    I also open a ticket to redux-toolkit and i received this message reduxjs/redux-toolkit#5041 (comment)

  2. LadyBluenotes commented on Jul 15, 2026

    @LadyBluenotes
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions