-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PrivateRoute component for checking auth
- Loading branch information
Justin Campbell
committed
Nov 13, 2020
1 parent
50f03ec
commit 4e79718
Showing
3 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./PrivateRoute"; |