Propper error handling, when initial user auth failed

This commit is contained in:
SleepWalker 2016-11-20 11:36:15 +02:00
parent 9e970e690b
commit 251a1237b4
3 changed files with 16 additions and 10 deletions

View File

@ -22,6 +22,12 @@ import { setLocale } from 'components/i18n/actions';
export function authenticate({token, refreshToken}) {
return (dispatch) =>
authentication.validateToken({token, refreshToken})
.catch(() => {
// TODO: log this case
dispatch(logout());
return Promise.reject();
})
.then(({token, refreshToken}) =>
accounts.current({token})
.then((user) => ({
@ -34,11 +40,7 @@ export function authenticate({token, refreshToken}) {
refreshToken
}
}))
, () => {
dispatch(logout());
return Promise.reject();
})
)
.then(({user, account}) => {
dispatch(add(account));
dispatch(activate(account));

View File

@ -2,7 +2,6 @@ import { routeActions } from 'react-router-redux';
import accounts from 'services/api/accounts';
import { logoutAll } from 'components/accounts/actions';
import authentication from 'services/api/authentication';
import { setLocale } from 'components/i18n/actions';
export const UPDATE = 'USER_UPDATE';

View File

@ -22,16 +22,21 @@ export function factory(store) {
request.addMiddleware(refreshTokenMiddleware(store));
request.addMiddleware(bearerHeaderMiddleware(store));
promise = new Promise((resolve, reject) => {
promise = Promise.resolve().then(() => {
const {user, accounts} = store.getState();
if (accounts.active || user.token) {
// authorizing user if it is possible
return store.dispatch(authenticate(accounts.active || user)).then(resolve, reject);
return store.dispatch(authenticate(accounts.active || user));
}
// auto-detect guests language
store.dispatch(changeLang(user.lang)).then(resolve, reject);
return Promise.reject();
}).catch(() => {
// the user is guest or user authentication failed
const {user} = store.getState();
// auto-detect guest language
return store.dispatch(changeLang(user.lang));
});
return promise;