diff --git a/src/store/index.js b/src/store/index.js index 46133f8..c8acda2 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -16,7 +16,6 @@ ******************************************************************************/ import { configureStore } from "@reduxjs/toolkit"; -import userReducer from './userSlice'; import icReducer from './icSlice'; import configReducer from './configSlice' import { apiSlice } from "./apiSlice"; @@ -25,7 +24,6 @@ import authReducer from './authSlice'; export const store = configureStore({ reducer: { auth: authReducer, - user: userReducer, infrastructure: icReducer, config: configReducer, [apiSlice.reducerPath]: apiSlice.reducer, diff --git a/src/store/userSlice.js b/src/store/userSlice.js deleted file mode 100644 index 7293e4b..0000000 --- a/src/store/userSlice.js +++ /dev/null @@ -1,97 +0,0 @@ -/** - * This file is part of VILLASweb. - * - * VILLASweb is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * VILLASweb is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with VILLASweb. If not, see . - ******************************************************************************/ - -import {createSlice, createAsyncThunk} from '@reduxjs/toolkit' -import RestAPI from '../common/api/rest-api'; - -const userSlice = createSlice({ - name: 'user', - initialState: { - currentUser: null, - currentToken: null, - isLoading: false, - loginMessage: ''}, - extraReducers: (builder) => { - builder - .addCase(login.pending, (state, action) => { - state.isLoading = true - }) - .addCase(login.fulfilled, (state, action) => { - state.isLoading = false - state.currentUser = action.payload.user - state.currentToken = action.payload.token - - localStorage.setItem('currentUser', JSON.stringify(action.payload.user)); - localStorage.setItem('token', action.payload.token); - }) - .addCase(login.rejected, (state, action) => { - state.loginMessage = 'Wrong credentials! Please try again.' - }) - .addCase(logout.pending, (state) => { - state.currentUser = null - state.currentToken = null - }) - .addCase(loginExternal.pending, (state, action) => { - state.isLoading = true - }) - .addCase(loginExternal.fulfilled, (state, action) => { - state.isLoading = false - state.currentUser = action.payload.user - state.currentToken = action.payload.token - - localStorage.setItem('currentUser', JSON.stringify(action.payload.user)); - localStorage.setItem('token', action.payload.token); - }) - } -}) - -export const login = createAsyncThunk( - 'user/login', - async (userData, thunkAPI) => { - try { - const res = await RestAPI.post('/api/v2/authenticate/internal', userData) - return {user: res.user, token: res.token} - } catch(error) { - console.log('Error while trying to log in: ', error) - return thunkAPI.rejectWithValue(error) - } - } -) - -export const loginExternal = createAsyncThunk( - 'user/loginExternal', - async (data, thunkAPI) => { - try { - const res = await RestAPI.post(this.makeURL('/authenticate/external'), null, null, 60000) - - return {user: res.user, token: res.token} - } catch(error) { - console.log('Error while trying to log in externally: ', error) - return thunkAPI.rejectWithValue(error) - } - } -) - -export const logout = createAsyncThunk( - 'user/logout', - async () => { - //remove token and current user from local storage - localStorage.clear(); - } -) - -export default userSlice.reducer \ No newline at end of file