defined protect function for access control on front end

This commit is contained in:
Mikayla Dobson
2022-11-23 10:49:55 -06:00
parent 7cc2851de7
commit d2d38bf7dd
8 changed files with 96 additions and 65 deletions

View File

@@ -0,0 +1,29 @@
import { useContext } from "react";
import { useNavigate } from "react-router-dom";
import { Button, Page } from "../components/ui";
import Divider from "../components/ui/Divider";
import { AuthContext } from "../context/AuthContext";
export default function Protect({ children = <></> }) {
const { user } = useContext(AuthContext);
const navigate = useNavigate();
if (!user) {
return (
<Page>
<div className="content-unauthorized">
<h1>Hi there! You don't look too familiar.</h1>
<p>To view the content on this page, please log in below:</p>
<Divider />
<Button onClick={() => navigate('/login')}>Log In</Button>
</div>
</Page>
)
} else {
return (
<Page>
{ children }
</Page>
)
}
}