Last active
March 23, 2017 03:10
-
-
Save wyqydsyq/f8978dc600e27b3fc02eb7811c58bcaf to your computer and use it in GitHub Desktop.
login.ts
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 characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment