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}) {
 | 
			
		||||
    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));
 | 
			
		||||
 
 | 
			
		||||
@@ -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';
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user