Guest User

Untitled

a guest
Feb 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import React from "react";
  2. import Route from "react-router-dom/Route";
  3. import Redirect from "react-router-dom/Redirect";
  4. import { connect } from "react-redux";
  5. import withRouter from "react-router-dom/withRouter";
  6. import { verifyToken } from '../actions/index'
  7.  
  8. class PrivateRouteContainer extends React.Component {
  9.  
  10. componentDidMount() {
  11. this.props.verifyToken();
  12. }
  13.  
  14. render() {
  15. const {
  16. isAuthenticated,
  17. component: Component,
  18. location,
  19. ...props
  20. } = this.props;
  21. return (
  22. <Route
  23. {...props}
  24. render={props =>
  25. isAuthenticated ? (
  26. <Component {...props} />
  27. ) : (
  28. <Redirect
  29. to={{
  30. pathname: "/login",
  31. state: { from: location }
  32. }}
  33. />
  34. )}
  35. />
  36. );
  37. }
  38. }
  39.  
  40. const mapStateToProps = state => ({
  41. isAuthenticated: state.auth.isAuthenticated
  42. });
  43.  
  44. const mapDispatchToProps = dispatch => ({
  45. verifyToken: () => {
  46. dispatch(verifyToken())
  47. }
  48. })
  49.  
  50. export default withRouter(connect(mapStateToProps, mapDispatchToProps)(PrivateRouteContainer));
Add Comment
Please, Sign In to add comment