49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
/* Redux toolkit middleware handles updates immutably, but eslint doesn't know that */
|
|
/* eslint-disable no-param-reassign */
|
|
import { createSlice } from '@reduxjs/toolkit';
|
|
|
|
export const CURRENT_VERSION = 5;
|
|
|
|
const INITIAL_STATE = {
|
|
version: CURRENT_VERSION,
|
|
accountCache: {
|
|
isLoggedIn: false,
|
|
isChecking: true,
|
|
username: undefined,
|
|
userEmail: undefined,
|
|
userRole: undefined,
|
|
},
|
|
};
|
|
|
|
export const accountSlice = createSlice({
|
|
name: 'account',
|
|
initialState: INITIAL_STATE,
|
|
reducers: {
|
|
setAuthChecking: (state, action) => {
|
|
state.accountCache.isChecking = action.payload;
|
|
},
|
|
setLoggedIn: (state, action) => {
|
|
state.accountCache.isLoggedIn = true;
|
|
state.accountCache.isChecking = false;
|
|
state.accountCache.username = action.payload.username;
|
|
state.accountCache.userEmail = action.payload.email;
|
|
state.accountCache.userRole = action.payload.role;
|
|
},
|
|
setLoggedOut: state => {
|
|
state.accountCache.isLoggedIn = false;
|
|
state.accountCache.isChecking = false;
|
|
state.accountCache.username = undefined;
|
|
state.accountCache.userEmail = undefined;
|
|
state.accountCache.userRole = undefined;
|
|
},
|
|
reset: () => INITIAL_STATE,
|
|
},
|
|
});
|
|
|
|
// Don't cache anything across sessions, session cookie handles it
|
|
export const loadState = () => INITIAL_STATE;
|
|
|
|
export const { setAuthChecking, setLoggedIn, setLoggedOut, reset } = accountSlice.actions;
|
|
|
|
export default accountSlice.reducer;
|