Feat(Frontend): Added Routing
This commit is contained in:
22
frontend/src/components/RequireAuth.tsx
Normal file
22
frontend/src/components/RequireAuth.tsx
Normal 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;
|
16
frontend/src/components/ScrollToTop.tsx
Normal file
16
frontend/src/components/ScrollToTop.tsx
Normal 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;
|
Reference in New Issue
Block a user