2016-06-04 19:16:39 +05:30
|
|
|
import request from 'services/request';
|
2016-10-30 17:42:49 +05:30
|
|
|
import accounts from 'services/api/accounts';
|
2016-06-04 19:16:39 +05:30
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
const authentication = {
|
2016-06-04 19:16:39 +05:30
|
|
|
login({
|
|
|
|
login = '',
|
|
|
|
password = '',
|
|
|
|
rememberMe = false
|
|
|
|
}) {
|
|
|
|
return request.post(
|
|
|
|
'/api/authentication/login',
|
2016-12-25 23:39:47 +05:30
|
|
|
{login, password, rememberMe},
|
|
|
|
{token: null}
|
2016-06-04 19:16:39 +05:30
|
|
|
);
|
2016-06-04 20:28:29 +05:30
|
|
|
},
|
|
|
|
|
2016-11-15 11:25:15 +05:30
|
|
|
/**
|
|
|
|
* @param {object} options
|
|
|
|
* @param {object} [options.token] - an optional token to overwrite headers
|
|
|
|
* in middleware and disable token auto-refresh
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
logout(options = {}) {
|
|
|
|
return request.post('/api/authentication/logout', {}, {
|
|
|
|
token: options.token
|
|
|
|
});
|
2016-06-28 15:43:27 +05:30
|
|
|
},
|
|
|
|
|
2016-07-28 10:33:30 +05:30
|
|
|
forgotPassword({
|
|
|
|
login = ''
|
|
|
|
}) {
|
|
|
|
return request.post(
|
|
|
|
'/api/authentication/forgot-password',
|
|
|
|
{login}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
recoverPassword({
|
|
|
|
key = '',
|
|
|
|
newPassword = '',
|
|
|
|
newRePassword = ''
|
|
|
|
}) {
|
|
|
|
return request.post(
|
|
|
|
'/api/authentication/recover-password',
|
|
|
|
{key, newPassword, newRePassword}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
/**
|
|
|
|
* Resolves if token is valid
|
|
|
|
*
|
|
|
|
* @param {object} options
|
|
|
|
* @param {string} options.token
|
|
|
|
* @param {string} options.refreshToken
|
|
|
|
*
|
|
|
|
* @return {Promise} - resolves with options.token or with a new token
|
2017-01-06 11:34:14 +05:30
|
|
|
* if it was refreshed. As a side effect the response
|
|
|
|
* will have a `user` field with current user data
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
|
|
|
validateToken({token, refreshToken}) {
|
2016-11-05 15:41:41 +05:30
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (typeof token !== 'string') {
|
|
|
|
throw new Error('token must be a string');
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
})
|
2016-11-08 12:00:53 +05:30
|
|
|
.then(() => accounts.current({token}))
|
2017-01-06 11:34:14 +05:30
|
|
|
.then((user) => ({token, refreshToken, user}))
|
2016-11-05 15:41:41 +05:30
|
|
|
.catch((resp) => {
|
|
|
|
if (resp.message === 'Token expired') {
|
|
|
|
return authentication.requestToken(refreshToken)
|
2017-01-06 11:34:14 +05:30
|
|
|
.then(({token}) =>
|
|
|
|
accounts.current({token})
|
|
|
|
.then((user) => ({token, refreshToken, user}))
|
|
|
|
);
|
2016-11-05 15:41:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
},
|
|
|
|
|
2016-08-10 00:47:49 +05:30
|
|
|
/**
|
|
|
|
* Request new access token using a refreshToken
|
|
|
|
*
|
|
|
|
* @param {string} refreshToken
|
|
|
|
*
|
|
|
|
* @return {Promise} - resolves to {token}
|
|
|
|
*/
|
|
|
|
requestToken(refreshToken) {
|
2016-06-04 20:28:29 +05:30
|
|
|
return request.post(
|
|
|
|
'/api/authentication/refresh-token',
|
2016-12-13 01:37:49 +05:30
|
|
|
{refresh_token: refreshToken}, // eslint-disable-line
|
|
|
|
{token: null}
|
2016-08-10 00:47:49 +05:30
|
|
|
).then((resp) => ({
|
|
|
|
token: resp.access_token
|
|
|
|
}));
|
2016-06-04 19:16:39 +05:30
|
|
|
}
|
|
|
|
};
|
2016-11-05 15:41:41 +05:30
|
|
|
|
|
|
|
export default authentication;
|