From 4e79718e1ea1779adbac9871fadfa839c74cc58d Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Fri, 13 Nov 2020 15:21:51 -0500 Subject: [PATCH] Create PrivateRoute component for checking auth --- src/components/PrivateRoute/PrivateRoute.js | 24 +++++++++++++++++++++ src/components/PrivateRoute/PrivateRoute.md | 1 + src/components/PrivateRoute/index.js | 1 + 3 files changed, 26 insertions(+) create mode 100644 src/components/PrivateRoute/PrivateRoute.js create mode 100644 src/components/PrivateRoute/PrivateRoute.md create mode 100644 src/components/PrivateRoute/index.js diff --git a/src/components/PrivateRoute/PrivateRoute.js b/src/components/PrivateRoute/PrivateRoute.js new file mode 100644 index 0000000..8507e82 --- /dev/null +++ b/src/components/PrivateRoute/PrivateRoute.js @@ -0,0 +1,24 @@ +import React from 'react'; +import PropTypes from "prop-types"; +import { Route, Redirect } from 'react-router-dom'; +import { useLogin } from "../AuthProvider/"; + +export default function PrivateRoute({ children, ...rest }) { + const isLoggedIn = useLogin(); + console.log("isLoggedIn", isLoggedIn); + + return ( + + { + isLoggedIn + ? children + : + } + + ); +}; + +PrivateRoute.propTypes = { + /** The route's path. */ + "path": PropTypes.string.isRequired +}; \ No newline at end of file diff --git a/src/components/PrivateRoute/PrivateRoute.md b/src/components/PrivateRoute/PrivateRoute.md new file mode 100644 index 0000000..7f0d5c0 --- /dev/null +++ b/src/components/PrivateRoute/PrivateRoute.md @@ -0,0 +1 @@ +The PrivateRoute wraps [React Router](https://reactrouter.com/)'s [Route component](https://reactrouter.com/web/api/Route) and checks for authentication using [AuthProvider](#/Components/AuthProvider). If authentication is valid, the children of the PrivateRoute are rendered. Otherwise, the user is redirected to the login page. \ No newline at end of file diff --git a/src/components/PrivateRoute/index.js b/src/components/PrivateRoute/index.js new file mode 100644 index 0000000..4c9765d --- /dev/null +++ b/src/components/PrivateRoute/index.js @@ -0,0 +1 @@ +export { default } from "./PrivateRoute"; \ No newline at end of file