All files / src/components auth0-lock.js

58.33% Statements 28/48
50% Branches 4/8
25% Functions 2/8
48.15% Lines 13/27
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 55 562x 2x 2x   2x 12x   2x   2x                                           2x               2x       2x           2x   2x     2x  
import {PropTypes} from 'react'
import {connect} from 'react-redux'
import {push} from 'react-router-redux'
 
import {lock, setAuth0User} from '../auth0'
import Pure from './pure'
 
const localStorage = window.localStorage
 
class Auth0 extends Pure {
  static propTypes = {
    push: PropTypes.func.isRequired,
    setAuth0User: PropTypes.func.isRequired
  }
 
  _authenticated = (authResult) => {
    const {push, setAuth0User} = this.props
    lock.getProfile(authResult.idToken, (error, profile) => {
      if (error) {
        setAuth0User(null)
        push('/login')
      } else {
        const user = {
          ...authResult,
          profile
        }
        localStorage.setItem('user', JSON.stringify(user))
        setAuth0User(user)
        push('/')
      }
    })
  }
 
  componentDidMount () {
    // when testing, auth0 credentials are not currently entered, so `lock` will be null
    if (lock) {
      lock.show()
      lock.on('authenticated', this._authenticated)
    }
  }
 
  render () {
    return null
  }
}
 
function mapStateToProps (state) {
  return state
}
const mapDispatchToProps = {
  push,
  setAuth0User
}
 
export default connect(mapStateToProps, mapDispatchToProps)(Auth0)