35 lines
774 B
JavaScript
35 lines
774 B
JavaScript
import React, { Component } from "react";
|
|
import { connect } from "react-redux";
|
|
import { Redirect, Route } from "react-router-dom";
|
|
|
|
class PrivateRoute extends Component {
|
|
render() {
|
|
const { isAuthenticated, component: Component, ...props } = this.props;
|
|
return (
|
|
<Route
|
|
{...props}
|
|
render={props =>
|
|
isAuthenticated ? (
|
|
<Component {...props} />
|
|
) : (
|
|
<Redirect
|
|
to={{
|
|
pathname: "/login",
|
|
state: { from: props.location }
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
|
|
function mapStateToProps(state) {
|
|
return {
|
|
isAuthenticated: state.auth.isAuthenticated
|
|
};
|
|
}
|
|
|
|
export default connect(mapStateToProps)(PrivateRoute);
|