Created
July 13, 2020 10:52
-
-
Save mtreacy002/037e91889a5787c95e05c21dc689cae4 to your computer and use it in GitHub Desktop.
Protected page with Reactjs
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
import React, { useContext } from "react"; | |
import { Route, Redirect } from "react-router-dom"; | |
import { AuthContext } from "./AuthContext"; | |
export default function ProtectedRoute({ children, ...rest }) { | |
const { isAuth } = useContext(AuthContext); | |
return ( | |
<Route | |
{...rest} | |
render={({ location }) => { | |
return isAuth ? ( | |
children | |
) : ( | |
<Redirect | |
to={{ | |
pathname: "/login", | |
state: { from: location } | |
}} | |
/> | |
); | |
}} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment