Skip to content

Commit

Permalink
Persist redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorBeck committed Sep 2, 2024
1 parent beb322f commit 8e2e34a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/redux/slices/authSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,25 @@ const authSlice = createSlice({
},
logout: state => {
state.user = null;
state.idToken = null; // Clear idToken
state.isAuthenticated = false;
state.loading = false;
state.error = null;
},
clearError: state => {
state.error = null;
}
},
reset: state => initialState // Add reset action
}
});

export const { loginStart, loginSuccess, loginFailure, logout, clearError } =
authSlice.actions;
export const {
loginStart,
loginSuccess,
loginFailure,
logout,
clearError,
reset
} = authSlice.actions;

export default authSlice.reducer;
37 changes: 34 additions & 3 deletions src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
import { configureStore } from '@reduxjs/toolkit';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER
} from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // defaults to localStorage for web
import { combineReducers } from 'redux';
import authReducer from './slices/authSlice';
import chickenReducer from './slices/chickenSlice';

const persistConfig = {
key: 'root',
storage
};

const rootReducer = combineReducers({
auth: authReducer,
chickens: chickenReducer
});

const persistedReducer = persistReducer(persistConfig, rootReducer);

export const store = configureStore({
reducer: {
auth: authReducer
}
reducer: persistedReducer,
middleware: getDefaultMiddleware =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER]
}
})
});

export const persistor = persistStore(store);

0 comments on commit 8e2e34a

Please sign in to comment.