2016-10-30 17:42:49 +05:30
|
|
|
import authentication from 'services/api/authentication';
|
|
|
|
import accounts from 'services/api/accounts';
|
|
|
|
import { updateUser, logout } from 'components/user/actions';
|
2016-11-05 15:41:41 +05:30
|
|
|
import { setLocale } from 'components/i18n/actions';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} Account
|
|
|
|
* @property {string} account.id
|
|
|
|
* @property {string} account.username
|
|
|
|
* @property {string} account.email
|
|
|
|
* @property {string} account.token
|
|
|
|
* @property {string} account.refreshToken
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Account|object} account
|
|
|
|
* @param {string} account.token
|
|
|
|
* @param {string} account.refreshToken
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
|
|
|
* @return {function}
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
|
|
|
export function authenticate({token, refreshToken}) {
|
2016-11-08 12:00:53 +05:30
|
|
|
return (dispatch) =>
|
|
|
|
authentication.validateToken({token, refreshToken})
|
2016-10-30 17:42:49 +05:30
|
|
|
.then(({token, refreshToken}) =>
|
|
|
|
accounts.current({token})
|
|
|
|
.then((user) => ({
|
|
|
|
user,
|
|
|
|
account: {
|
|
|
|
id: user.id,
|
|
|
|
username: user.username,
|
|
|
|
email: user.email,
|
|
|
|
token,
|
|
|
|
refreshToken
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
.then(({user, account}) => {
|
|
|
|
dispatch(add(account));
|
|
|
|
dispatch(activate(account));
|
2016-11-05 15:41:41 +05:30
|
|
|
dispatch(updateUser({
|
|
|
|
isGuest: false,
|
|
|
|
...user
|
|
|
|
}));
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
return dispatch(setLocale(user.lang))
|
|
|
|
.then(() => account);
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Account} account
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
|
|
|
* @return {function}
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
|
|
|
export function revoke(account) {
|
|
|
|
return (dispatch, getState) => {
|
2016-11-13 02:01:44 +05:30
|
|
|
const accountToReplace = getState().accounts.available.find(({id}) => id !== account.id);
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
if (accountToReplace) {
|
|
|
|
return dispatch(authenticate(accountToReplace))
|
2016-11-15 11:25:15 +05:30
|
|
|
.then(() => {
|
|
|
|
authentication.logout(account);
|
|
|
|
dispatch(remove(account));
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
2016-11-08 12:00:53 +05:30
|
|
|
|
|
|
|
return dispatch(logout());
|
2016-10-30 17:42:49 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ADD = 'accounts:add';
|
|
|
|
/**
|
|
|
|
* @api private
|
|
|
|
*
|
|
|
|
* @param {Account} account
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
|
|
|
* @return {object} - action definition
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
|
|
|
export function add(account) {
|
|
|
|
return {
|
|
|
|
type: ADD,
|
|
|
|
payload: account
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const REMOVE = 'accounts:remove';
|
|
|
|
/**
|
|
|
|
* @api private
|
|
|
|
*
|
|
|
|
* @param {Account} account
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
|
|
|
* @return {object} - action definition
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
|
|
|
export function remove(account) {
|
|
|
|
return {
|
|
|
|
type: REMOVE,
|
|
|
|
payload: account
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ACTIVATE = 'accounts:activate';
|
|
|
|
/**
|
|
|
|
* @api private
|
|
|
|
*
|
|
|
|
* @param {Account} account
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
|
|
|
* @return {object} - action definition
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
|
|
|
export function activate(account) {
|
|
|
|
return {
|
|
|
|
type: ACTIVATE,
|
|
|
|
payload: account
|
|
|
|
};
|
|
|
|
}
|
2016-11-05 15:41:41 +05:30
|
|
|
|
2016-11-15 11:25:15 +05:30
|
|
|
export function logoutAll() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const {accounts: {available}} = getState();
|
|
|
|
|
|
|
|
available.forEach((account) => authentication.logout(account));
|
|
|
|
|
|
|
|
dispatch(reset());
|
|
|
|
};
|
|
|
|
}
|
2016-11-14 10:58:25 +05:30
|
|
|
export const RESET = 'accounts:reset';
|
|
|
|
/**
|
2016-11-15 11:25:15 +05:30
|
|
|
* @api private
|
|
|
|
*
|
2016-11-14 10:58:25 +05:30
|
|
|
* @return {object} - action definition
|
|
|
|
*/
|
|
|
|
export function reset() {
|
|
|
|
return {
|
|
|
|
type: RESET
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
export const UPDATE_TOKEN = 'accounts:updateToken';
|
|
|
|
/**
|
|
|
|
* @param {string} token
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
|
|
|
* @return {object} - action definition
|
2016-11-05 15:41:41 +05:30
|
|
|
*/
|
|
|
|
export function updateToken(token) {
|
|
|
|
return {
|
|
|
|
type: UPDATE_TOKEN,
|
|
|
|
payload: token
|
|
|
|
};
|
|
|
|
}
|