skip to Main Content

I am trying to reset everything when the user logout from the app. But I can’t find a way to delete my persist configs.

Here is my implementation

reducers

import { combineReducers } from 'redux';
import { persistReducer } from 'redux-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';
import createSensitiveStorage from 'redux-persist-sensitive-storage';

import appReducer from '../app';
import authReducer from '../auth';
import notificationsReducer from '../notifications';
import businessReducer from '../businesses';
import pendingPostReducer from '../pendingPost';
import postsReducer from '../posts';
import otherUsersReducer from '../otherUsers';
import searchHistoryReducer from '../searchHistory';
import rentRequestReducer from '../rentRequests';
import visitRequestsReducer from '../visitRequests';
import commonReducer from '../common';
import conversations from '../conversations';
import messages from '../messages';
import reviews from '../reviews';
const sensitiveStorage = createSensitiveStorage({
  keychainService: 'myKeychain',
  sharedPreferencesName: 'mySharedPrefs',
});

const appPersistConfig = {
  key: 'app',
  storage: AsyncStorage,
};

const authPersistConfig = {
  key: 'auth',
  storage: sensitiveStorage,
};

const searchHistoryPersistConfig = {
  key: 'searchHistory',
  storage: AsyncStorage,
};

const combinedReducer = combineReducers({
  app: persistReducer(appPersistConfig, appReducer),
  auth: persistReducer(authPersistConfig, authReducer),
  searchHistory: persistReducer(
    searchHistoryPersistConfig,
    searchHistoryReducer
  ),
  otherUsers: otherUsersReducer,
  notifications: notificationsReducer,
  business: businessReducer,
  pendingPost: pendingPostReducer,
  rentRequests: rentRequestReducer,
  posts: postsReducer,
  visitRequests: visitRequestsReducer,
  common: commonReducer,
  conversations: conversations,
  messages: messages,
  reviews: reviews,
});

const rootReducer = (state, action) => {
  if (action.type === 'auth/logout/fulfilled') {
    return combinedReducer(undefined, action);
  }
  return combinedReducer(state, action);
};

export default rootReducer;

store

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import { composeWithDevTools } from 'remote-redux-devtools';
import { persistStore } from 'redux-persist';

import rootReducer from './reducers';

const loggerMiddleware = createLogger({
  predicate: (getstate, actions) => __DEV__,
});

const store = createStore(
  rootReducer,
  composeWithDevTools(applyMiddleware(thunk, loggerMiddleware))
);

export let persistedStore = persistStore(store);
export default store;

2

Answers


  1. If the issue that you are passing undefined to the root reducer and this is just rehydrating the state then an alternative may be to "overwrite" the authReducer auth state so these are persisted.

    Example:

    const logout = createAsyncThunk(
      "auth/logout",
      async () => { .... },
    );
    
    const initialState = {
      ....
    };
    
    const authSlice = createSlice({
      name: "auth",
      initialState,
      reducers: {
        ....
      },
      extraReducers: builder => {
        builder.addCase(logout.fulfilled, (state, action) => {
          ... overwrite/nullify/etc auth state properties ...
          ... or return initialState
        });
      },
    });
    
    Login or Signup to reply.
  2. Assume that you want to purge all cached data when user logged out.

    
    const rootReducer = (state, action) => {
      if (action.type === "app/purgeStore") {
        // Clean-up redux-persist's storage engine
        AsyncStorage.removeItem("persist:root");
    
        state = {};
      }
    
      return reducer(state, action);
    };
    
    // Later in UI screen/hooks
    const logout = () => {
      // Logout user logic here ....
      //...
    
      // Finally purge all your persisted data
      dispatch({ type: "app/purgeStore" });
    };
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search