Skip to content

Reset cache on logout #1622

Closed
Closed
@KevinBrustolin

Description

@KevinBrustolin

Hi,

I'm facing an issue in order to clear state and cache after a user logout.

Here, what I'm doing right now to handle that :

import { combineReducers, configureStore } from '@reduxjs/toolkit';
import { api } from './api';
import authReducer, { logout } from '../features/auth/authSlice';
import requesterReducer from '../features/requester/requesterSlice';
import { unauthorizedMiddleware } from './store/middlewares/unauthorized';

const combinedReducer = combineReducers({
    [api.reducerPath]: api.reducer,
    auth: authReducer,
    requester: requesterReducer,
});
export const store = configureStore({
    reducer: (rootState, action) => {
        let state = rootState;
        if (logout.match(action)) {
            state = undefined;
        }
        return combinedReducer(state, action);
    },
   
    middleware: (getDefaultMiddleware) => [
        ...getDefaultMiddleware().concat(api.middleware),
        unauthorizedMiddleware,
    ],
});

The issue comes in App.js :

function App() {
    const {isLoading} = useGetSessionQuery();
    if (isLoading) return <Loader />;

    return (
        <Router>
            <div className="App">
                <Switch>
                    <Route exact path="/login" component={Login} />
                    <PrivateRoute path="/">
                        <Requester />
                    </PrivateRoute>
                </Switch>
            </div>
        </Router>
    );
}

Reseting state like I do provokes isLoading from my getSession query to be 'true' so my app becomes stuck until I refresh.

Thanks!

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