From aae41a1d30f37511fc1b893f1b60787b7db1b99f Mon Sep 17 00:00:00 2001 From: "Dr. Sergey Pogodin" Date: Thu, 5 Jan 2017 04:40:06 +0100 Subject: [PATCH] Additional fix for the social login case --- src/routes/index.js | 9 ++++++--- src/services/AuthService.js | 32 ++++++++++++++++++-------------- src/store/modules/global.js | 5 +++++ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/routes/index.js b/src/routes/index.js index 0d9e590..8d15875 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -25,6 +25,8 @@ import ProviderDetailsRoute from './ProviderDetails'; import ResetPasswordRoute from './ResetPassword'; import {defaultAuth0Service} from '../services/AuthService'; +import {onSocialLoginSuccessAction} from 'store/modules/global'; + export const createRoutes = (store) => ({ path: '/', name: 'CoreLayout', @@ -32,9 +34,10 @@ export const createRoutes = (store) => ({ onEnter: (nextState, replace, cb) => { // parse the hash if present if (nextState.location.hash) { - defaultAuth0Service.parseHash(nextState.location.hash); - replace('/dashboard'); - cb(); + defaultAuth0Service.parseHash(nextState.location.hash).then(() => { + store.dispatch(onSocialLoginSuccessAction()); + cb(); + }); } else { replace('/dashboard'); cb(); diff --git a/src/services/AuthService.js b/src/services/AuthService.js index e9c70e9..8ea883b 100644 --- a/src/services/AuthService.js +++ b/src/services/AuthService.js @@ -64,23 +64,27 @@ class AuthService { if (authResult && authResult.idToken) { _self.setToken(authResult.idToken); // get social profile - _self.getProfile((error, profile) => { - if (error) { - // remove the id token - _self.removeToken(); - throw error; - } else { - userApi.registerSocialUser(profile.name, profile.email, _self.getToken()).then( - (authResult) => { - localStorage.setItem('userInfo', JSON.stringify(authResult)); - }).catch((reason) => { + return new Promise((resolve) => { + _self.getProfile((error, profile) => { + if (error) { // remove the id token - _self.removeToken(); - throw reason; - }); - } + _self.removeToken(); + throw error; + } else { + return userApi.registerSocialUser(profile.name, profile.email, _self.getToken()).then( + (authResult2) => { + localStorage.setItem('userInfo', JSON.stringify(authResult2)); + resolve(authResult2); + }).catch((reason) => { + // remove the id token + _self.removeToken(); + throw reason; + }); + } + }); }); } + return Promise.reject(new Error('Social login failure')); } /** diff --git a/src/store/modules/global.js b/src/store/modules/global.js index 4e06868..9e34afa 100644 --- a/src/store/modules/global.js +++ b/src/store/modules/global.js @@ -72,6 +72,11 @@ export const loginAction = (data) => (dispatch) => { }); }; +export const onSocialLoginSuccessAction = () => (dispatch) => { + dispatch({type: LOGIN_ACTION_SUCCESS}); + browserHistory.push(LOGIN_REDIRECT[loadUserInfo().user.role]); +}; + export const logoutAction = () => (dispatch) => { browserHistory.push('/home'); dispatch({