2016-05-01 16:01:40 +05:30
|
|
|
import { routeActions } from 'react-router-redux';
|
|
|
|
|
2016-02-26 11:55:47 +05:30
|
|
|
import request from 'services/request';
|
2016-05-01 15:58:54 +05:30
|
|
|
import accounts from 'services/api/accounts';
|
2016-05-20 01:11:43 +05:30
|
|
|
import { setLocale } from 'components/i18n/actions';
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export const UPDATE = 'USER_UPDATE';
|
|
|
|
/**
|
|
|
|
* @param {string|Object} payload jwt token or user object
|
|
|
|
* @return {Object} action definition
|
|
|
|
*/
|
|
|
|
export function updateUser(payload) {
|
|
|
|
return {
|
|
|
|
type: UPDATE,
|
|
|
|
payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-20 01:11:43 +05:30
|
|
|
export const CHANGE_LANG = 'USER_CHANGE_LANG';
|
|
|
|
export function changeLang(lang) {
|
|
|
|
return (dispatch, getState) => dispatch(setLocale(lang))
|
|
|
|
.then((lang) => {
|
|
|
|
const {user: {isGuest, lang: oldLang}} = getState();
|
|
|
|
|
|
|
|
if (!isGuest && oldLang !== lang) {
|
|
|
|
accounts.changeLang(lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: CHANGE_LANG,
|
|
|
|
payload: {
|
|
|
|
lang
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
export const SET = 'USER_SET';
|
|
|
|
export function setUser(payload) {
|
|
|
|
return {
|
|
|
|
type: SET,
|
|
|
|
payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logout() {
|
2016-05-29 00:53:34 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch(setUser({
|
|
|
|
lang: getState().user.lang,
|
|
|
|
isGuest: true
|
|
|
|
}));
|
2016-05-01 16:01:40 +05:30
|
|
|
dispatch(routeActions.push('/login'));
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
|
|
|
export function fetchUserData() {
|
|
|
|
return (dispatch) =>
|
2016-05-01 15:58:54 +05:30
|
|
|
accounts.current()
|
2016-02-26 11:55:47 +05:30
|
|
|
.then((resp) => {
|
|
|
|
dispatch(updateUser(resp));
|
2016-05-20 01:11:43 +05:30
|
|
|
|
|
|
|
return dispatch(changeLang(resp.lang));
|
2016-06-04 14:47:06 +05:30
|
|
|
});
|
|
|
|
/*
|
2016-02-26 11:55:47 +05:30
|
|
|
.catch((resp) => {
|
|
|
|
{
|
|
|
|
"name": "Unauthorized",
|
|
|
|
"message": "You are requesting with an invalid credential.",
|
|
|
|
"code": 0,
|
|
|
|
"status": 401,
|
|
|
|
"type": "yii\\web\\UnauthorizedHttpException"
|
|
|
|
}
|
|
|
|
});
|
2016-06-04 14:47:06 +05:30
|
|
|
*/
|
2016-02-26 11:55:47 +05:30
|
|
|
}
|
2016-02-26 23:43:41 +05:30
|
|
|
|
2016-03-16 11:24:42 +05:30
|
|
|
export function changePassword({
|
|
|
|
password = '',
|
|
|
|
newPassword = '',
|
2016-04-25 03:00:10 +05:30
|
|
|
newRePassword = '',
|
|
|
|
logoutAll = true,
|
2016-03-16 11:24:42 +05:30
|
|
|
}) {
|
|
|
|
return (dispatch) =>
|
2016-05-01 15:58:54 +05:30
|
|
|
accounts.changePassword(
|
2016-04-25 03:00:10 +05:30
|
|
|
{password, newPassword, newRePassword, logoutAll}
|
2016-03-16 11:24:42 +05:30
|
|
|
)
|
|
|
|
.then((resp) => {
|
|
|
|
dispatch(updateUser({
|
|
|
|
shouldChangePassword: false
|
|
|
|
}));
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:43:41 +05:30
|
|
|
|
2016-06-04 19:16:39 +05:30
|
|
|
export function authenticate(token, refreshToken) {
|
2016-02-26 23:43:41 +05:30
|
|
|
if (!token || token.split('.').length !== 3) {
|
|
|
|
throw new Error('Invalid token');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (dispatch) => {
|
|
|
|
request.setAuthToken(token);
|
2016-06-04 19:16:39 +05:30
|
|
|
|
|
|
|
return dispatch(fetchUserData()).then((resp) => {
|
|
|
|
dispatch(updateUser({
|
|
|
|
isGuest: false,
|
|
|
|
token,
|
|
|
|
refreshToken
|
|
|
|
}));
|
|
|
|
return resp;
|
|
|
|
});
|
2016-02-26 23:43:41 +05:30
|
|
|
};
|
|
|
|
}
|