Skip to content

Commit

Permalink
Create PrivateRoute component for checking auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Campbell committed Nov 13, 2020
1 parent 50f03ec commit 4e79718
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/components/PrivateRoute/PrivateRoute.js
Original file line number Diff line number Diff line change
@@ -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 (
<Route {...rest}>
{
isLoggedIn
? children
: <Redirect to="/login"/>
}
</Route>
);
};

PrivateRoute.propTypes = {
/** The route's path. */
"path": PropTypes.string.isRequired
};
1 change: 1 addition & 0 deletions src/components/PrivateRoute/PrivateRoute.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions src/components/PrivateRoute/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./PrivateRoute";

0 comments on commit 4e79718

Please sign in to comment.