blob: 77cde56c4a09820328e6f3adc890c5bcb2c33086 [file] [log] [blame]
2230113338fd3882025-06-06 23:33:57 +08001import React from 'react';
2import { Navigate, Outlet } from 'react-router-dom';
3
4// Component to protect routes that require authentication
5const RequireAuth = () => {
6 // Check if userId cookie exists
7 const getCookie = (name) => {
8 const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
9 return match ? match[2] : null;
10 };
11
12 const userId = getCookie('userId');
13 // If not authenticated, redirect to login
14 return userId ? <Outlet /> : <Navigate to="/login" replace />;
15};
16
17export default RequireAuth;