Skip to content

Instantly share code, notes, and snippets.

@wyqydsyq
Last active March 23, 2017 03:10
Show Gist options
  • Select an option

  • Save wyqydsyq/f8978dc600e27b3fc02eb7811c58bcaf to your computer and use it in GitHub Desktop.

Select an option

Save wyqydsyq/f8978dc600e27b3fc02eb7811c58bcaf to your computer and use it in GitHub Desktop.

Revisions

  1. wyqydsyq revised this gist Mar 23, 2017. 1 changed file with 23 additions and 15 deletions.
    38 changes: 23 additions & 15 deletions login.ts
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,8 @@ const {
    } = userActions

    import {
    types as dialogTypes
    types as dialogTypes,
    actions as dialogActions
    } from '../actions/dialogs'

    let lock
    @@ -26,24 +27,20 @@ const getLock = () => {
    } else {
    lock = new Auth0Lock(
    'xxx',
    'x.au.auth0.com',
    'xxx.au.auth0.com',
    {
    auth: {
    redirect: false,
    sso: false,
    responseType: 'token',
    params: {
    scope: 'openid email user_metadata app_metadata picture'
    }
    }
    }
    )

    return lock
    }
    }

    export const showLogin = (auth): Promise<any> => new Promise((resolve, reject) => {
    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()
    auth.show(showOpts)
    })

    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) {
    @@ -81,9 +74,9 @@ export function loginEpic(action$, store): Observable<any> {

    // handle user attempting to login
    action$.ofType(dialogTypes.showLogin)
    .mergeMap(() => {
    .mergeMap(action => {
    return Observable
    .fromPromise(showLogin(getLock()))
    .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))
    hideLogin(getLock())
    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
    }
    })
    })
    )
    }
  2. wyqydsyq revised this gist Mar 22, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions login.ts
    Original 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'
  3. wyqydsyq created this gist Mar 22, 2017.
    112 changes: 112 additions & 0 deletions login.ts
    Original 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