2016-09-19 23:43:35 +00:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2016-09-20 18:17:31 +00:00
|
|
|
import { isEqual } from 'lodash';
|
|
|
|
import { push } from 'react-router-redux';
|
2016-10-17 13:27:22 +00:00
|
|
|
|
2016-12-06 16:55:48 +00:00
|
|
|
import paths from 'router/paths';
|
|
|
|
import redirectLocationInterface from 'interfaces/redirect_location';
|
|
|
|
import { setRedirectLocation } from 'redux/nodes/redirectLocation/actions';
|
|
|
|
import userInterface from 'interfaces/user';
|
|
|
|
import Footer from 'components/Footer';
|
2016-09-19 23:43:35 +00:00
|
|
|
|
|
|
|
export class AuthenticatedRoutes extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.element,
|
2016-09-20 18:17:31 +00:00
|
|
|
dispatch: PropTypes.func,
|
|
|
|
loading: PropTypes.bool.isRequired,
|
2016-10-21 23:13:41 +00:00
|
|
|
locationBeforeTransitions: redirectLocationInterface,
|
|
|
|
user: userInterface,
|
2016-09-19 23:43:35 +00:00
|
|
|
};
|
|
|
|
|
2016-09-20 18:17:31 +00:00
|
|
|
componentWillMount () {
|
|
|
|
const { loading, user } = this.props;
|
2016-10-03 17:54:22 +00:00
|
|
|
const { redirectToLogin, redirectToPasswordReset } = this;
|
2016-09-20 18:17:31 +00:00
|
|
|
|
2016-10-19 20:22:18 +00:00
|
|
|
if (!loading && !user) {
|
|
|
|
return redirectToLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user && user.force_password_reset) {
|
|
|
|
return redirectToPasswordReset();
|
|
|
|
}
|
2016-09-20 18:17:31 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (isEqual(this.props, nextProps)) return false;
|
|
|
|
|
|
|
|
const { loading, user } = nextProps;
|
2016-10-03 17:54:22 +00:00
|
|
|
const { redirectToLogin, redirectToPasswordReset } = this;
|
2016-09-20 18:17:31 +00:00
|
|
|
|
2016-10-19 20:22:18 +00:00
|
|
|
if (!loading && !user) {
|
|
|
|
return redirectToLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user && user.force_password_reset) {
|
|
|
|
return redirectToPasswordReset();
|
|
|
|
}
|
2016-09-20 18:17:31 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectToLogin = () => {
|
2016-10-17 13:27:22 +00:00
|
|
|
const { dispatch, locationBeforeTransitions } = this.props;
|
2016-09-20 18:17:31 +00:00
|
|
|
const { LOGIN } = paths;
|
|
|
|
|
2016-10-17 13:27:22 +00:00
|
|
|
dispatch(setRedirectLocation(locationBeforeTransitions));
|
2016-09-20 18:17:31 +00:00
|
|
|
return dispatch(push(LOGIN));
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:54:22 +00:00
|
|
|
redirectToPasswordReset = () => {
|
|
|
|
const { dispatch } = this.props;
|
|
|
|
const { RESET_PASSWORD } = paths;
|
|
|
|
|
|
|
|
return dispatch(push(RESET_PASSWORD));
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:43:35 +00:00
|
|
|
render () {
|
|
|
|
const { children, user } = this.props;
|
|
|
|
|
2016-10-19 20:22:18 +00:00
|
|
|
if (!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-19 23:43:35 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{children}
|
2016-12-06 16:55:48 +00:00
|
|
|
<Footer />
|
2016-09-19 23:43:35 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
2016-09-20 18:17:31 +00:00
|
|
|
const { loading, user } = state.auth;
|
2016-10-17 13:27:22 +00:00
|
|
|
const { locationBeforeTransitions } = state.routing;
|
2016-09-19 23:43:35 +00:00
|
|
|
|
2016-10-17 13:27:22 +00:00
|
|
|
return { loading, locationBeforeTransitions, user };
|
2016-09-19 23:43:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(AuthenticatedRoutes);
|