yapi/client/components/AuthenticatedComponent.js
System Administrator 362172ef52 fix: bug修复
2017-09-14 18:19:13 +08:00

52 lines
1.1 KiB
JavaScript
Executable File

import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types'
import { changeMenuItem } from '../reducer/modules/menu'
@connect(
(state) => {
return{
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>
)
}
}
}