I'm trying to save the reducer auth when i login. My problem is not all values in auth i want to save when i login, i only want to save infos and token.
Pls check my code below authReducer
const authReducer = (state = initialState, action) => {
switch (action.type) {
case authConstants.LOGIN_REQUEST:
return {
...state,
token: null,
infos: null,
isLoggedIn: false,
errors: null,
isLoading: true,
};
case authConstants.LOGIN_SUCCESS:
return {
...state,
token: action.payload.token,
infos: action.payload.user[0],
isLoggedIn: true,
errors: null,
isLoading: false,
};
case authConstants.LOGIN_FAILED:
return {
...state,
token: null,
infos: null,
isLoggedIn: false,
errors: null,
isLoading: false,
};
case authConstants.LOGOUT:
return {
...state,
token: null,
infos: null,
isLoggedIn: false,
errors: null,
isLoading: false,
};
default:
return state;
}
};
export default authReducer;
index
import auth from './authReducer';
import { combineReducers } from 'redux';
import user from './userReducer';
const combinedReducer = combineReducers({
auth,
user,
});
const rootReducer = (state, action) => {
if (action.type === 'Auth/LOGOUT') {
state = undefined;
}
return combinedReducer(state, action);
};
export default rootReducer;
store.js
// Redux
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
// Redux Dev Tools
import { composeWithDevTools } from 'redux-devtools-extension';
// Redux Persist
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
const persistConfig = {
key: 'auth',
storage: storage,
whitelist: ['auth.infos', 'auth.token'], // which reducer want to store
};
const pReducer = persistReducer(persistConfig, rootReducer);
const middleware = [thunk];
const store = createStore(pReducer, composeWithDevTools(applyMiddleware(...middleware)));
const persistor = persistStore(store);
export { persistor, store };