Feat(Frontend): Added Routing

This commit is contained in:
2023-07-21 22:37:28 -04:00
parent a73afc90d9
commit 71cb9bdff6
7 changed files with 97 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
import { useContext } from "react";
import { Navigate, useLocation } from "react-router-dom";
import { AuthContext } from "src/AuthContext";
interface IProps {
children: React.ReactNode;
}
const RequireAuth = ({ children }: IProps) => {
const { authenticated } = useContext(AuthContext);
const location = useLocation();
if (authenticated) {
return <>{children}</>;
} else {
// re-route user back to login page if not logged in
return <Navigate state={{ from: location }} to="/login/" />;
}
};
export default RequireAuth;

View File

@@ -0,0 +1,16 @@
import { useEffect } from "react";
import { useLocation } from "react-router";
const ScrollToTop = () => {
const { pathname } = useLocation();
// pathname changes => scroll to top
// scrolling only on navigation
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
};
export default ScrollToTop;