All files / src/components auth0-lock.js

100% Statements 51/51
80% Branches 8/10
100% Functions 8/8
100% Lines 26/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 553x 3x 3x   3x 3x 24x   3x               2x 2x 2x 2x 2x 2x 2x 1x 1x   1x       1x 1x 1x             2x 3x       2x       3x   3x     3x  
import {PropTypes} from 'react'
import {connect} from 'react-redux'
import {push} from 'react-router-redux'
 
import {setAuth0User} from '../actions/user'
import {defaultLockOptions, getLock} from '../auth0'
import Pure from './pure'
 
class Auth0 extends Pure {
  static propTypes = {
    lockOptions: PropTypes.object,
    push: PropTypes.func.isRequired,
    setAuth0User: PropTypes.func.isRequired
  }
 
  componentDidMount () {
    const {lockOptions, push, setAuth0User} = this.props
    const lock = getLock(lockOptions)
    lock.show()
    lock.on('authenticated', (authResult) => {
      lock.hide()
      lock.getProfile(authResult.idToken, (error, profile) => {
        if (error) {
          setAuth0User(null)
          push('/login')
        } else {
          const user = {
            ...authResult,
            profile
          }
          window.localStorage.setItem('user', JSON.stringify(user))
          setAuth0User(user)
          push('/')
        }
      })
    })
  }
 
  render () {
    return null
  }
}
 
function mapStateToProps (state, props) {
  return {
    lockOptions: props.lockOptions || defaultLockOptions
  }
}
const mapDispatchToProps = {
  push,
  setAuth0User
}
 
export default connect(mapStateToProps, mapDispatchToProps)(Auth0)