added landingpage and fixed routing
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
import { Navigate, Outlet } from "react-router-dom";
|
||||
import { useAtom } from "jotai";
|
||||
import { setIsLoggedInAtom } from "@/states/Atoms";
|
||||
import { Navigate, Outlet, useLocation } from "react-router-dom";
|
||||
import Cookies from "js-cookie";
|
||||
import { useContext } from "react";
|
||||
import { UserContext } from "@/states/Context";
|
||||
|
||||
export const ProtectedRoutes = () => {
|
||||
const [isLoggedIn] = useAtom(setIsLoggedInAtom);
|
||||
const user = useContext(UserContext);
|
||||
const location = useLocation();
|
||||
const hasToken = Boolean(Cookies.get("token"));
|
||||
|
||||
return isLoggedIn ? <Outlet /> : <Navigate to="/login" />;
|
||||
if (hasToken && !user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user ? (
|
||||
<Outlet />
|
||||
) : (
|
||||
<Navigate to="/login" replace state={{ from: location }} />
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user