I have been able to create a persistent local storage that allows the user to login and stores a randomly generated API token, which is necessary to communicate with the API.
The issue that I am having, is that my sign out button does not work anymore because the session store keeps the login information and token in the local storage and doesn't delete unless I manually do it.
Below is my code.
import { createStore, applyMiddleware, compose } from "redux";
import rootReducer from "../reducers";
import thunk from "redux-thunk";
function saveToLocalStorage(state) {
try {
const serializedState = JSON.stringify(state);
localStorage.setItem("state", serializedState);
} catch (e) {
console.log(e);
}
}
function loadFromLocalStorage() {
try {
const serializedState = localStorage.getItem("state");
if (serializedState === null) return undefined;
return JSON.parse(serializedState);
} catch (e) {
console.log(e);
return undefined;
}
}
const storeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
|| compose;
const persistedState = loadFromLocalStorage();
const store = createStore(
rootReducer,
persistedState,
storeEnhancers(applyMiddleware(thunk))
);
store.subscribe(() => saveToLocalStorage(store.getState()));
export default store;