This commit is contained in:
rgrgogu
2026-05-05 23:16:22 +08:00
parent 683b25956a
commit 745c51682e
125 changed files with 18925 additions and 2 deletions
+18
View File
@@ -0,0 +1,18 @@
import { Navigate, Outlet } from 'react-router-dom'
import { useAuth } from '../contexts/AuthContext'
import Unauthorized from '@/modules/public/pages/Unauthorized'
import { useEffect } from 'react'
export default function ProtectedRoute({ allowedRoles = [] }) {
const { user, loading } = useAuth() // ← just loading and user, nothing else
if (loading) return null // ← only block on initial cold load
if (!user) return <Navigate to="/login" replace />
if (allowedRoles.length > 0 && !allowedRoles.includes(user.acc_type)) {
return <Unauthorized /> // ← show unauthorized instead of redirecting
}
return <Outlet />
}