0

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 };
2
  • can you please provide more details. Commented Oct 7, 2020 at 3:26
  • @Paveloosha. Just added it Commented Oct 7, 2020 at 3:29

1 Answer 1

1

First create a function that will help to persist/whitelist some part of the state. i.e.

import storage from "redux-persist/lib/storage";
import { persistReducer } from "redux-persist";

export default function persist(key, whitelist, reducer) {
  return persistReducer(
    {
      key,
      storage,
      whitelist
    },
    reducer
  );
}

After that you can use the function in the authReducer.

import persist from 'path/to/persist';

const authReducer = (state = initialState, action) => { 
  // other code 
}
 
export default persist('auth', ['infos', 'token'], authReducer);

And now you can remove persistConfig and persistReducer from store.

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 } from 'redux-persist';

const middleware = [thunk];
const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(...middleware)));
const persistor = persistStore(store);
export { persistor, store };

Credit

Sign up to request clarification or add additional context in comments.

10 Comments

It still doesn't save the token and infos :(
Just added my index reducer. I don't know if could help
can you add how you used the persist function in authReducer
Did you rebuild your app ?
Hi! yes. I just noticed, if i do this, it works export default persist('auth', ['token'], authReducer);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.