feat: Implement authentication flow with token verification and protected routes

This commit is contained in:
2025-10-24 20:45:37 +02:00
parent b99f52f09a
commit 960e91c38a
4 changed files with 50 additions and 42 deletions

View File

@@ -1,37 +1,9 @@
import { Outlet, Navigate } from "react-router-dom";
import { Navigate, Outlet } from "react-router-dom";
import { useAtom } from "jotai";
import { setIsLoggedInAtom } from "@/states/Atoms";
import { useEffect } from "react";
import Cookies from "js-cookie";
const API_BASE =
(import.meta as any).env?.VITE_BACKEND_URL ||
import.meta.env.VITE_BACKEND_URL ||
"http://localhost:8002";
export const ProtectedRoutes = () => {
const [isLoggedIn, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
useEffect(() => {
if (Cookies.get("token")) {
const verifyToken = async () => {
const response = await fetch(`${API_BASE}/api/verifyToken`, {
method: "GET",
headers: {
Authorization: `Bearer ${Cookies.get("token")}`,
},
});
if (response.ok) {
setIsLoggedIn(true);
} else {
Cookies.remove("token");
setIsLoggedIn(false);
window.location.reload();
}
};
verifyToken();
}
}, []);
const [isLoggedIn] = useAtom(setIsLoggedInAtom);
return isLoggedIn ? <Outlet /> : <Navigate to="/login" />;
};