feat: migrate authentication storage to use js-cookie for improved token management

This commit is contained in:
estifanos
2026-07-22 09:51:00 +00:00
parent e862d5cd8e
commit a76549b018
5 changed files with 25 additions and 38 deletions

View File

@@ -1,3 +1,4 @@
import Cookies from 'js-cookie';
import { Navigate, Outlet, useLocation } from 'react-router-dom';
import type { ReactNode } from 'react';
import { authStorage } from '../utils/auth-storage';
@@ -7,14 +8,9 @@ interface ProtectedRouteProps {
loginPath?: string;
}
function getTokenFromCookie(): string | undefined {
const match = document.cookie.match(/(?:^|;\s*)auth-token=([^;]*)/);
return match ? decodeURIComponent(match[1]) : undefined;
}
export function ProtectedRoute({ children, loginPath = '/login' }: ProtectedRouteProps) {
const location = useLocation();
const token = authStorage.getToken() ?? getTokenFromCookie();
const token = authStorage.getToken() ?? Cookies.get('auth-token');
if (!token) {
return <Navigate to={loginPath} state={{ from: location }} replace />;