private-router.js 425 B

1234567891011121314151617181920
  1. import React from "react";
  2. import { Route, Redirect } from "react-router-dom";
  3. export const PrivateRoute = ({ component: Component, protectedRoute, ...rest }) => (
  4. <Route
  5. {...rest}
  6. render={props => {
  7. if (protectedRoute) {
  8. const user = localStorage.getItem("user");
  9. if (!user) {
  10. return <Redirect to="/" />;
  11. }
  12. return <Component {...props} />;
  13. }
  14. return <Component {...props} />;
  15. }}
  16. />
  17. );