2017-11-29 05:24:35 +01:00
|
|
|
const { connect } = require('react-redux')
|
|
|
|
const PropTypes = require('prop-types')
|
2017-12-04 07:24:30 +01:00
|
|
|
const { Redirect } = require('react-router-dom')
|
2017-11-29 05:24:35 +01:00
|
|
|
const h = require('react-hyperscript')
|
2017-12-04 07:24:30 +01:00
|
|
|
const MetamaskRoute = require('./metamask-route')
|
|
|
|
const { UNLOCK_ROUTE, INITIALIZE_ROUTE } = require('../../routes')
|
2017-11-29 05:24:35 +01:00
|
|
|
|
|
|
|
const Authenticated = ({ component: Component, isUnlocked, isInitialized, ...props }) => {
|
2017-12-04 07:24:30 +01:00
|
|
|
const component = renderProps => {
|
2017-11-29 05:24:35 +01:00
|
|
|
switch (true) {
|
|
|
|
case isUnlocked:
|
2017-12-04 07:24:30 +01:00
|
|
|
return h(Component, { ...renderProps })
|
2017-11-29 05:24:35 +01:00
|
|
|
case !isInitialized:
|
2017-12-04 07:24:30 +01:00
|
|
|
return h(Redirect, { to: { pathname: INITIALIZE_ROUTE } })
|
2017-11-29 05:24:35 +01:00
|
|
|
default:
|
|
|
|
return h(Redirect, { to: { pathname: UNLOCK_ROUTE } })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-12-04 07:24:30 +01:00
|
|
|
h(MetamaskRoute, {
|
2017-11-29 05:24:35 +01:00
|
|
|
...props,
|
2017-12-04 07:24:30 +01:00
|
|
|
component,
|
2017-11-29 05:24:35 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
Authenticated.propTypes = {
|
|
|
|
component: PropTypes.func,
|
|
|
|
isUnlocked: PropTypes.bool,
|
|
|
|
isInitialized: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const { metamask: { isUnlocked, isInitialized } } = state
|
|
|
|
return {
|
|
|
|
isUnlocked,
|
|
|
|
isInitialized,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = connect(mapStateToProps)(Authenticated)
|