Last active
March 23, 2017 03:10
-
-
Save wyqydsyq/f8978dc600e27b3fc02eb7811c58bcaf to your computer and use it in GitHub Desktop.
Revisions
-
wyqydsyq revised this gist
Mar 23, 2017 . 1 changed file with 23 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,8 @@ const { } = userActions import { types as dialogTypes, actions as dialogActions } from '../actions/dialogs' let lock @@ -26,24 +27,20 @@ const getLock = () => { } else { lock = new Auth0Lock( 'xxx', 'xxx.au.auth0.com', { auth: { responseType: 'token', params: { scope: 'openid email user_metadata app_metadata picture' } } } ) } } export const showLogin = (auth, showOpts?: {}): Promise<any> => new Promise((resolve, reject) => { auth.on('authenticated', result => { resolve(result) }) @@ -56,13 +53,9 @@ export const showLogin = (auth): Promise<any> => new Promise((resolve, reject) = reject(error) }) auth.show(showOpts) }) export const getProfile = (auth, token): Promise<any> => new Promise((res, rej) => auth.getUserInfo(token, (error, result) => { if (error) { @@ -81,9 +74,9 @@ export function loginEpic(action$, store): Observable<any> { // handle user attempting to login action$.ofType(dialogTypes.showLogin) .mergeMap(action => { return Observable .fromPromise(showLogin(getLock(), action.payload)) .map(loginSuccess) .catch(error => { logError(error) @@ -103,9 +96,24 @@ export function loginEpic(action$, store): Observable<any> { }) .map(payload => { dispatch(setUser(payload)) getLock().hide() }) .mapTo(push('/sessions')) }), // handle failed login action$.ofType(userTypes.loginFail) .map(action => { // set lock to undefined so a new instance will be created when // calling showLogin with the errors lock = undefined return dialogActions.showLogin({ rememberLastLogin: false, flashMessage: { type: 'error', text: action.payload.error_description } }) }) ) } -
wyqydsyq revised this gist
Mar 22, 2017 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,6 +30,7 @@ const getLock = () => { { auth: { redirect: false, sso: false, responseType: 'token', params: { scope: 'openid email user_metadata app_metadata picture' -
wyqydsyq created this gist
Mar 22, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,112 @@ import Auth0Lock from 'auth0-lock' import { Observable } from 'rxjs' import { push } from 'react-router-redux' import { error as logError } from '../../util/logger' import { types as userTypes, actions as userActions } from '../actions/user' const { loginSuccess, loginFail, setUser, getProfileFail } = userActions import { types as dialogTypes } from '../actions/dialogs' let lock const getLock = () => { if (lock) { return lock } else { lock = new Auth0Lock( 'xxx', 'x.au.auth0.com', { auth: { redirect: false, responseType: 'token', params: { scope: 'openid email user_metadata app_metadata picture' } } } ) return lock } } export const showLogin = (auth): Promise<any> => new Promise((resolve, reject) => { auth.on('authenticated', result => { resolve(result) }) auth.on('unrecoverable_error', error => { reject(error) }) auth.on('authorization_error', error => { reject(error) }) auth.show() }) export const hideLogin = (auth): void => { auth.hide() } export const getProfile = (auth, token): Promise<any> => new Promise((res, rej) => auth.getUserInfo(token, (error, result) => { if (error) { return rej(error) } else { return res(result) } }) ) export function loginEpic(action$, store): Observable<any> { const dispatch = store.dispatch // start with an empty stream that merges child streams return Observable.empty().merge( // handle user attempting to login action$.ofType(dialogTypes.showLogin) .mergeMap(() => { return Observable .fromPromise(showLogin(getLock())) .map(loginSuccess) .catch(error => { logError(error) dispatch(loginFail(error)) return Observable.empty() }) }), // handle successful login action$.ofType(userTypes.loginSuccess) .mergeMap(action => { return Observable .fromPromise(getProfile(getLock(), action.payload.accessToken)) .catch(error => { dispatch(getProfileFail(error)) return Observable.empty() }) .map(payload => { dispatch(setUser(payload)) hideLogin(getLock()) }) .mapTo(push('/sessions')) }) ) } export default loginEpic