mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-05-31 14:11:58 +05:30
Propper error handling, when initial user auth failed
This commit is contained in:
@ -22,6 +22,12 @@ import { setLocale } from 'components/i18n/actions';
|
|||||||
export function authenticate({token, refreshToken}) {
|
export function authenticate({token, refreshToken}) {
|
||||||
return (dispatch) =>
|
return (dispatch) =>
|
||||||
authentication.validateToken({token, refreshToken})
|
authentication.validateToken({token, refreshToken})
|
||||||
|
.catch(() => {
|
||||||
|
// TODO: log this case
|
||||||
|
dispatch(logout());
|
||||||
|
|
||||||
|
return Promise.reject();
|
||||||
|
})
|
||||||
.then(({token, refreshToken}) =>
|
.then(({token, refreshToken}) =>
|
||||||
accounts.current({token})
|
accounts.current({token})
|
||||||
.then((user) => ({
|
.then((user) => ({
|
||||||
@ -34,11 +40,7 @@ export function authenticate({token, refreshToken}) {
|
|||||||
refreshToken
|
refreshToken
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
, () => {
|
)
|
||||||
dispatch(logout());
|
|
||||||
|
|
||||||
return Promise.reject();
|
|
||||||
})
|
|
||||||
.then(({user, account}) => {
|
.then(({user, account}) => {
|
||||||
dispatch(add(account));
|
dispatch(add(account));
|
||||||
dispatch(activate(account));
|
dispatch(activate(account));
|
||||||
|
@ -2,7 +2,6 @@ import { routeActions } from 'react-router-redux';
|
|||||||
|
|
||||||
import accounts from 'services/api/accounts';
|
import accounts from 'services/api/accounts';
|
||||||
import { logoutAll } from 'components/accounts/actions';
|
import { logoutAll } from 'components/accounts/actions';
|
||||||
import authentication from 'services/api/authentication';
|
|
||||||
import { setLocale } from 'components/i18n/actions';
|
import { setLocale } from 'components/i18n/actions';
|
||||||
|
|
||||||
export const UPDATE = 'USER_UPDATE';
|
export const UPDATE = 'USER_UPDATE';
|
||||||
|
@ -22,16 +22,21 @@ export function factory(store) {
|
|||||||
request.addMiddleware(refreshTokenMiddleware(store));
|
request.addMiddleware(refreshTokenMiddleware(store));
|
||||||
request.addMiddleware(bearerHeaderMiddleware(store));
|
request.addMiddleware(bearerHeaderMiddleware(store));
|
||||||
|
|
||||||
promise = new Promise((resolve, reject) => {
|
promise = Promise.resolve().then(() => {
|
||||||
const {user, accounts} = store.getState();
|
const {user, accounts} = store.getState();
|
||||||
|
|
||||||
if (accounts.active || user.token) {
|
if (accounts.active || user.token) {
|
||||||
// authorizing user if it is possible
|
// 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
|
return Promise.reject();
|
||||||
store.dispatch(changeLang(user.lang)).then(resolve, 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;
|
return promise;
|
||||||
|
Reference in New Issue
Block a user