2017-07-24 11:35:31 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { changeMenuItem } from '../actions/menu'
|
|
|
|
|
2017-07-26 21:24:02 +08:00
|
|
|
@connect(
|
|
|
|
(state) => {
|
|
|
|
return{
|
|
|
|
isAuthenticated: state.login.isLogin
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
changeMenuItem
|
|
|
|
}
|
|
|
|
)
|
2017-07-24 11:35:31 +08:00
|
|
|
export function requireAuthentication(Component) {
|
2017-07-26 21:24:02 +08:00
|
|
|
return class AuthenticatedComponent extends React.Component {
|
2017-07-24 11:35:31 +08:00
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
}
|
|
|
|
static propTypes ={
|
|
|
|
isAuthenticated : PropTypes.bool,
|
|
|
|
location: PropTypes.object,
|
|
|
|
dispatch: PropTypes.func,
|
|
|
|
history: PropTypes.object,
|
|
|
|
changeMenuItem:PropTypes.func
|
|
|
|
}
|
|
|
|
componentWillMount() {
|
|
|
|
this.checkAuth();
|
|
|
|
}
|
|
|
|
componentWillReceiveProps() {
|
|
|
|
this.checkAuth();
|
|
|
|
}
|
|
|
|
checkAuth() {
|
|
|
|
if( !this.props.isAuthenticated ){
|
|
|
|
this.props.history.push('/');
|
|
|
|
this.props.changeMenuItem('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.props.isAuthenticated
|
|
|
|
? <Component {...this.props}/>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|