mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-05-31 14:11:58 +05:30
#246: remove redundant /current calls during account authentication
This commit is contained in:
@@ -25,28 +25,27 @@ import logger from 'services/logger';
|
||||
export function authenticate({token, refreshToken}) {
|
||||
return (dispatch) =>
|
||||
authentication.validateToken({token, refreshToken})
|
||||
.catch(() => {
|
||||
// TODO: log this case
|
||||
dispatch(logoutAll());
|
||||
.catch((resp) => {
|
||||
logger.warn('Error validating token during auth', {
|
||||
resp
|
||||
});
|
||||
|
||||
return Promise.reject();
|
||||
return dispatch(logoutAll())
|
||||
.then(() => Promise.reject());
|
||||
})
|
||||
.then(({token, refreshToken}) =>
|
||||
accounts.current({token})
|
||||
.then((user) => ({
|
||||
user: {
|
||||
isGuest: false,
|
||||
...user
|
||||
},
|
||||
account: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
token,
|
||||
refreshToken
|
||||
}
|
||||
}))
|
||||
)
|
||||
.then(({token, refreshToken, user}) => ({
|
||||
user: {
|
||||
isGuest: false,
|
||||
...user
|
||||
},
|
||||
account: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
token,
|
||||
refreshToken
|
||||
}
|
||||
}))
|
||||
.then(({user, account}) => {
|
||||
dispatch(add(account));
|
||||
dispatch(activate(account));
|
||||
|
@@ -55,7 +55,8 @@ const authentication = {
|
||||
* @param {string} options.refreshToken
|
||||
*
|
||||
* @return {Promise} - resolves with options.token or with a new token
|
||||
* if it was refreshed
|
||||
* if it was refreshed. As a side effect the response
|
||||
* will have a `user` field with current user data
|
||||
*/
|
||||
validateToken({token, refreshToken}) {
|
||||
return new Promise((resolve) => {
|
||||
@@ -66,11 +67,14 @@ const authentication = {
|
||||
resolve();
|
||||
})
|
||||
.then(() => accounts.current({token}))
|
||||
.then(() => ({token, refreshToken}))
|
||||
.then((user) => ({token, refreshToken, user}))
|
||||
.catch((resp) => {
|
||||
if (resp.message === 'Token expired') {
|
||||
return authentication.requestToken(refreshToken)
|
||||
.then(({token}) => ({token, refreshToken}));
|
||||
.then(({token}) =>
|
||||
accounts.current({token})
|
||||
.then((user) => ({token, refreshToken, user}))
|
||||
);
|
||||
}
|
||||
|
||||
return Promise.reject(resp);
|
||||
|
Reference in New Issue
Block a user