feat: route permission guards, home page per role — RolePermissionsContext, PermissionGuard, App routes, DB migration

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-20 10:50:22 +03:00
parent 80429cd52a
commit a701366216
5 changed files with 129 additions and 56 deletions

View File

@@ -0,0 +1,24 @@
import { Navigate } from 'react-router-dom'
import { useRolePermissions } from '../contexts/RolePermissionsContext'
interface Props {
permission: string
children: React.ReactNode
}
/**
* Protects a route by permission key.
* - While permissions are loading from DB: renders nothing (no flash).
* - If user lacks the permission: redirects to their role's home page.
*/
export function PermissionGuard({ permission, children }: Props) {
const { can, loading, userHomePage } = useRolePermissions()
if (loading) return null
if (!can(permission)) {
return <Navigate to={userHomePage} replace />
}
return <>{children}</>
}