mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
WIP
This commit is contained in:
35
client/src/store/authentication/authentication.actions.js
Normal file
35
client/src/store/authentication/authentication.actions.js
Normal 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
34
client/src/store/authentication/authentication.reducer.js
Normal file
34
client/src/store/authentication/authentication.reducer.js
Normal 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);
|
||||
};
|
||||
8
client/src/store/authentication/authentication.types.js
Normal file
8
client/src/store/authentication/authentication.types.js
Normal 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',
|
||||
};
|
||||
Reference in New Issue
Block a user