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
+7
View File
@@ -0,0 +1,7 @@
// src/routes/AppRouter.jsx
import { RouterProvider } from 'react-router-dom'
import { router } from './router' // your createBrowserRouter config stays in router.jsx
export default function AppRouter() {
return <RouterProvider router={router} />
}
+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 />
}
+20
View File
@@ -0,0 +1,20 @@
import { Navigate, Outlet } from 'react-router-dom'
import { useAuth } from '../contexts/AuthContext'
import { useEffect } from 'react'
export default function PublicRoute() {
const { user, loading } = useAuth() // ← just loading and user
if (loading) return null // ← only block on initial cold load
if (user) {
switch (user.acc_type) {
case 'admin': return <Navigate to="/admin" replace />
case 'client': return <Navigate to="/client" replace />
case 'staff': return <Navigate to="/staff" replace />
default: return <Navigate to="/admin" replace />
}
}
return <Outlet />
}
+14
View File
@@ -0,0 +1,14 @@
import { createBrowserRouter } from 'react-router-dom'
import { AdminRoutes } from '../modules/admin/routes/AdminRoutes'
import { ClientRoutes } from '../modules/client/routes/ClientRoutes'
import { StaffRoutes } from '../modules/staff/routes/StaffRoutes'
import { AuthRoutes } from '../modules/auth/routes/AuthRoutes'
import NotFound from '@/modules/public/pages/NotFound'
export const router = createBrowserRouter([
AuthRoutes, // "/" → Login, guarded by PublicRoute
AdminRoutes, // "/admin" → guarded by ProtectedRoute allowedRoles=['admin']
ClientRoutes, // "/client" → guarded by ProtectedRoute allowedRoles=['client']
StaffRoutes, // "/staff" → guarded by ProtectedRoute allowedRoles=['staff']
{ path: '*', element: <NotFound /> },
])