2017-08-01 20:28:03 -07:00
|
|
|
import { actions as notifActions } from 'redux-notifications';
|
2018-07-23 21:25:49 -04:00
|
|
|
import { currentBackend } from 'src/backend';
|
2017-08-01 20:28:03 -07:00
|
|
|
|
|
|
|
const { notifSend } = notifActions;
|
2016-02-25 12:31:21 -08:00
|
|
|
|
|
|
|
export const AUTH_REQUEST = 'AUTH_REQUEST';
|
|
|
|
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
|
|
|
|
export const AUTH_FAILURE = 'AUTH_FAILURE';
|
2017-11-11 12:30:24 -07:00
|
|
|
export const AUTH_REQUEST_DONE = 'AUTH_REQUEST_DONE';
|
2016-11-01 14:35:20 +01:00
|
|
|
export const LOGOUT = 'LOGOUT';
|
2016-02-25 12:31:21 -08:00
|
|
|
|
|
|
|
export function authenticating() {
|
|
|
|
return {
|
2016-11-01 14:35:20 +01:00
|
|
|
type: AUTH_REQUEST,
|
2016-02-25 12:31:21 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authenticate(userData) {
|
|
|
|
return {
|
|
|
|
type: AUTH_SUCCESS,
|
2016-11-01 14:35:20 +01:00
|
|
|
payload: userData,
|
2016-02-25 12:31:21 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authError(error) {
|
|
|
|
return {
|
|
|
|
type: AUTH_FAILURE,
|
|
|
|
error: 'Failed to authenticate',
|
|
|
|
payload: error,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-11 12:30:24 -07:00
|
|
|
export function doneAuthenticating() {
|
|
|
|
return {
|
|
|
|
type: AUTH_REQUEST_DONE,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-01 14:35:20 +01:00
|
|
|
export function logout() {
|
|
|
|
return {
|
|
|
|
type: LOGOUT,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:23:42 -02:00
|
|
|
// Check if user data token is cached and is valid
|
|
|
|
export function authenticateUser() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
dispatch(authenticating());
|
2018-08-07 14:46:54 -06:00
|
|
|
return backend
|
|
|
|
.currentUser()
|
|
|
|
.then(user => {
|
2017-08-31 13:46:02 -06:00
|
|
|
if (user) {
|
|
|
|
dispatch(authenticate(user));
|
|
|
|
} else {
|
2017-11-11 12:30:24 -07:00
|
|
|
dispatch(doneAuthenticating());
|
2017-08-31 13:46:02 -06:00
|
|
|
}
|
2017-01-26 19:23:42 -02:00
|
|
|
})
|
2018-08-07 14:46:54 -06:00
|
|
|
.catch(error => {
|
2017-01-26 19:23:42 -02:00
|
|
|
dispatch(authError(error));
|
|
|
|
dispatch(logoutUser());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
export function loginUser(credentials) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
|
|
|
|
dispatch(authenticating());
|
2018-08-07 14:46:54 -06:00
|
|
|
return backend
|
|
|
|
.authenticate(credentials)
|
|
|
|
.then(user => {
|
2016-11-01 14:35:20 +01:00
|
|
|
dispatch(authenticate(user));
|
2017-01-26 19:23:42 -02:00
|
|
|
})
|
2018-08-07 14:46:54 -06:00
|
|
|
.catch(error => {
|
2018-11-02 10:29:11 -04:00
|
|
|
console.error(error);
|
2018-08-07 14:46:54 -06:00
|
|
|
dispatch(
|
|
|
|
notifSend({
|
2018-12-12 00:50:23 +09:00
|
|
|
message: {
|
|
|
|
details: error.message,
|
|
|
|
key: 'ui.toast.onFailToAuth',
|
|
|
|
},
|
2018-08-07 14:46:54 -06:00
|
|
|
kind: 'warning',
|
|
|
|
dismissAfter: 8000,
|
|
|
|
}),
|
|
|
|
);
|
2017-01-26 19:23:42 -02:00
|
|
|
dispatch(authError(error));
|
2016-11-01 14:35:20 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logoutUser() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
2017-09-05 19:30:03 -07:00
|
|
|
Promise.resolve(backend.logout()).then(() => {
|
|
|
|
dispatch(logout());
|
|
|
|
});
|
2016-02-25 12:31:21 -08:00
|
|
|
};
|
|
|
|
}
|