useRoleGuard
Custom hook for Role-Based Access Control (RBAC).
Installation
npx sse-tool add use-role-guard
npm install sse-hooks
Usage
import { useRoleGuard } from "./{hooks file}";
const { hasAccess, user, isLoading } = useRoleGuard(["admin", "editor"], {
redirectTo: "/login",
});
if (isLoading) return <Loader />;
if (!hasAccess) return null; // Will redirect automatically
return <AdminPanel user={user} />;
import { useRoleGuard } from "sse-hooks";
const { hasAccess, user, isLoading } = useRoleGuard(["admin", "editor"], {
redirectTo: "/login",
});
if (isLoading) return <Loader />;
if (!hasAccess) return null; // Will redirect automatically
return <AdminPanel user={user} />;
API
Parameters
| Name | Type | Description |
|---|---|---|
| requiredRoles | string[] | The list of roles required to access the resource. |
| options | UseRoleGuardOptions | Configuration options including user object and redirect paths. |
Return Value
Returns UseRoleGuardReturn.
Access status, user data, and role checking utilities.
Changelog
31af5 — Refactor useBattery hook and remove utility functions
c165d — feat: enhance useRoleGuard and useSSR hooks with additional options and documentation
30da4 — feat: add new hooks and their metadata
2f161 — feat: add useConferenceSystem, useKbd, useMediaQuality, useNetworkInformation, usePortal, useRoleGuard, useScreenShare, useSymbol hooks