22301133 | 38fd388 | 2025-06-06 23:33:57 +0800 | [diff] [blame^] | 1 | import React from 'react'; |
| 2 | import { Navigate, Outlet } from 'react-router-dom'; |
| 3 | |
| 4 | // Component to protect routes that require authentication |
| 5 | const 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 | |
| 17 | export default RequireAuth; |