accounts-frontend/src/services/api/authentication.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

import request from 'services/request';
import accounts from 'services/api/accounts';
export default {
login({
login = '',
password = '',
rememberMe = false
}) {
return request.post(
'/api/authentication/login',
{login, password, rememberMe}
);
},
logout() {
return request.post('/api/authentication/logout');
},
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}
);
},
/**
* 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
* if it was refreshed
*/
validateToken({token, refreshToken}) {
// TODO: use refresh token to get fresh token. Dont forget, that it may be broken by refreshTokenMiddleware
// TODO: cover with tests
return accounts.current({token, autoRefreshToken: false})
.then(() => ({token, refreshToken}));
},
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) {
return request.post(
'/api/authentication/refresh-token',
2016-08-10 00:47:49 +05:30
{refresh_token: refreshToken}
).then((resp) => ({
token: resp.access_token
}));
}
};