2016-07-07 23:23:18 +02:00
|
|
|
import React, { Component, PropTypes } from "react";
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
2016-08-03 15:44:29 +02:00
|
|
|
// TODO: Handle expired session
|
2016-07-07 23:23:18 +02:00
|
|
|
export class RequireAuthentication extends Component {
|
|
|
|
componentWillMount () {
|
|
|
|
this.checkAuth(this.props.isAuthenticated);
|
|
|
|
}
|
|
|
|
|
2016-08-06 17:20:02 +02:00
|
|
|
componentWillUpdate (newProps) {
|
|
|
|
this.checkAuth(newProps.isAuthenticated);
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
checkAuth (isAuthenticated) {
|
|
|
|
if (!isAuthenticated) {
|
|
|
|
this.context.router.replace({
|
|
|
|
pathname: "/login",
|
|
|
|
state: {
|
|
|
|
nextPathname: this.props.location.pathname,
|
|
|
|
nextQuery: this.props.location.query
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.props.isAuthenticated === true
|
|
|
|
? this.props.children
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RequireAuthentication.propTypes = {
|
|
|
|
// Injected by React Router
|
|
|
|
children: PropTypes.node
|
|
|
|
};
|
|
|
|
|
|
|
|
RequireAuthentication.contextTypes = {
|
|
|
|
router: PropTypes.object.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => ({
|
|
|
|
isAuthenticated: state.auth.isAuthenticated
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(RequireAuthentication);
|