This commit is contained in:
Ahmed Bouhuolia
2020-03-16 00:06:15 +02:00
parent 56701951b7
commit 73711384f6
7925 changed files with 18478 additions and 959 deletions

View File

@@ -0,0 +1,35 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export default function login({ form }) {
return (dispatch) => {
return new Promise((resolve, reject) => {
ApiService.post('auth/login', form).then(response => {
const { data } = response;
dispatch({type: t.LOGIN_CLEAR_ERRORS});
if (data.token && data.user) {
dispatch({
type: t.LOGIN_SUCCESS,
user: data.user,
token: data.token,
});
}
resolve(response);
}).catch((error) => {
const { response } = error;
const { data } = response;
const { errors } = data;
dispatch({type: t.LOGIN_CLEAR_ERRORS});
if (errors){
dispatch({
type: t.LOGIN_FAILURE, errors,
});
}
reject(error);
});
});
}
}

View File

@@ -0,0 +1,34 @@
import {createReducer} from '@reduxjs/toolkit';
import t from 'store/types';
const initialState = {
token: '',
user: '',
locale: '',
errors: [],
};
export default createReducer(initialState, {
[t.LOGIN_SUCCESS]: (state, action) => {
state.token = action.token;
state.user = action.user;
},
[t.LOGIN_FAILURE]: (state, action) => {
state.errors = action.errors;
},
[t.LOGOUT]: (state) => {
state.token = '';
state.user = {};
},
[t.LOGIN_CLEAR_ERRORS]: (state) => {
state.errors = [];
},
});
export const isAuthenticated = (state) => !!state.authentication.token;
export const hasErrorType = (state, errorType) => {
return state.authentication.errors.find(e => e.type === errorType);
};

View File

@@ -0,0 +1,8 @@
export default {
LOGIN_REQUEST: 'LOGIN_REQUEST',
LOGIN_SUCCESS: 'LOGIN_SUCCESS',
LOGIN_FAILURE: 'LOGIN_FAILURE',
LOGOUT: 'LOGOUT',
LOGIN_CLEAR_ERRORS: 'LOGIN_CLEAR_ERRORS',
};