yapi/client/components/AuthenticatedComponent.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types'
2017-08-08 10:07:55 +08:00
import { changeMenuItem } from '../reducer/modules/menu'
@connect(
(state) => {
return{
2017-08-10 21:20:57 +08:00
isAuthenticated: state.user.isLogin
}
},
{
changeMenuItem
}
)
export function requireAuthentication(Component) {
return class AuthenticatedComponent extends React.Component {
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>
)
}
}
}