2016-02-26 11:55:47 +05:30
|
|
|
import request from 'services/request';
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SET = 'USER_SET';
|
|
|
|
export function setUser(payload) {
|
|
|
|
return {
|
|
|
|
type: SET,
|
|
|
|
payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logout() {
|
|
|
|
return setUser({isGuest: true});
|
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
|
|
|
export function fetchUserData() {
|
|
|
|
return (dispatch) =>
|
2016-02-26 11:59:42 +05:30
|
|
|
request.get('/api/accounts/current')
|
2016-02-26 11:55:47 +05:30
|
|
|
.then((resp) => {
|
|
|
|
dispatch(updateUser(resp));
|
|
|
|
})
|
|
|
|
.catch((resp) => {
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
"name": "Unauthorized",
|
|
|
|
"message": "You are requesting with an invalid credential.",
|
|
|
|
"code": 0,
|
|
|
|
"status": 401,
|
|
|
|
"type": "yii\\web\\UnauthorizedHttpException"
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
console.log(resp);
|
|
|
|
});
|
|
|
|
}
|
2016-02-26 23:43:41 +05:30
|
|
|
|
2016-03-16 11:24:42 +05:30
|
|
|
export function changePassword({
|
|
|
|
password = '',
|
|
|
|
newPassword = '',
|
|
|
|
newRePassword = ''
|
|
|
|
}) {
|
|
|
|
return (dispatch) =>
|
|
|
|
request.post(
|
|
|
|
'/api/accounts/change-password',
|
|
|
|
{password, newPassword, newRePassword}
|
|
|
|
)
|
|
|
|
.then((resp) => {
|
|
|
|
dispatch(updateUser({
|
|
|
|
shouldChangePassword: false
|
|
|
|
}));
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:43:41 +05:30
|
|
|
|
|
|
|
export function authenticate(token) {
|
|
|
|
if (!token || token.split('.').length !== 3) {
|
|
|
|
throw new Error('Invalid token');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (dispatch) => {
|
|
|
|
request.setAuthToken(token);
|
2016-03-02 02:06:14 +05:30
|
|
|
return dispatch(fetchUserData());
|
2016-02-26 23:43:41 +05:30
|
|
|
};
|
|
|
|
}
|