77 lines
1.4 KiB
JavaScript
77 lines
1.4 KiB
JavaScript
import { createSlice } from "@reduxjs/toolkit";
|
|
|
|
const initialState = {
|
|
token: localStorage.getItem("token"),
|
|
isAuthenticated: null,
|
|
loading: true,
|
|
user: null,
|
|
};
|
|
|
|
const authSlice = createSlice({
|
|
name: "auth",
|
|
initialState,
|
|
reducers: {
|
|
userLoaded(state, action) {
|
|
return {
|
|
...state,
|
|
isAuthenticated: true,
|
|
loading: false,
|
|
user: action.payload,
|
|
};
|
|
},
|
|
registerSuccess(state, action) {
|
|
return {
|
|
...state,
|
|
...action.payload,
|
|
isAuthenticated: true,
|
|
loading: false,
|
|
};
|
|
},
|
|
loginSucces(state, action) {
|
|
return {
|
|
...state,
|
|
...action.payload,
|
|
isAuthenticated: true,
|
|
loading: false,
|
|
};
|
|
},
|
|
accountDeleted(state, action) {
|
|
return {
|
|
...state,
|
|
token: null,
|
|
isAuthenticated: false,
|
|
loading: false,
|
|
user: null,
|
|
};
|
|
},
|
|
authError(state, action) {
|
|
return {
|
|
...state,
|
|
token: null,
|
|
isAuthenticated: false,
|
|
loading: false,
|
|
user: null,
|
|
};
|
|
},
|
|
logOut(state, action) {
|
|
return {
|
|
...state,
|
|
token: null,
|
|
isAuthenticated: false,
|
|
loading: false,
|
|
user: null,
|
|
};
|
|
},
|
|
},
|
|
});
|
|
export const {
|
|
logOut,
|
|
userLoaded,
|
|
loginSucces,
|
|
registerSuccess,
|
|
accountDeleted,
|
|
authError,
|
|
} = authSlice.actions;
|
|
|
|
export default authSlice.reducer;
|