AuthRoute.jsx 899 B

1234567891011121314151617181920212223242526272829303132333435
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import PropTypes from "prop-types";
  4. import { Redirect, Route } from "react-router-dom";
  5. @connect(state => ({
  6. loggedIn: state.user.get("loggedIn"),
  7. }))
  8. export default class AuthRoute extends Component {
  9. static propTypes = {
  10. loggedIn: PropTypes.bool,
  11. authRequired: PropTypes.bool,
  12. component: PropTypes.oneOfType([
  13. PropTypes.element,
  14. PropTypes.func,
  15. ]),
  16. }
  17. static defaultProps = {
  18. loggedIn: false,
  19. authRequired: true,
  20. component: () => {},
  21. }
  22. render() {
  23. const { authRequired } = this.props;
  24. if (this.props.loggedIn) {
  25. if (authRequired) return <Route props={ this.props } component={ this.props.component } />;
  26. return <Redirect to={ "/" } />;
  27. }
  28. if (authRequired) return <Redirect to={ "/login" } />;
  29. return <Route props={ this.props } component={ this.props.component } />;
  30. }
  31. }