useAdmin
Location: src/hooks/useAdmin.js
Overview
This file exports three hooks for admin authorization:
useAdmin-- checks whether the current user is an admin and returns their roleuseAdminPermission-- checks if the admin has a specific named permissionuseSuperAdmin-- convenience hook to check if the user is a superadmin
Admin status is checked via the get_admin_role RPC function first, with a fallback to a direct query on the admin_roles table if the function does not exist. The useAdmin hook re-checks on auth state changes (login/logout).
useAdmin
Signature
function useAdmin(): {
isAdmin: boolean,
adminRole: string | null,
isLoading: boolean,
error: string | null,
refetch: () => Promise<void>
}
Parameters
None.
Return Value
| Property | Type | Description |
|---|---|---|
isAdmin | boolean | true if the current user has any admin role. |
adminRole | string | null | The user's admin role string (e.g. "admin", "superadmin", "moderator"), or null if not an admin. |
isLoading | boolean | true while the admin check is in progress. |
error | string | null | Error message if the check failed. |
refetch | () => Promise<void> | Manually re-check admin status. |
Usage
function AdminGuard({ children }) {
const { isAdmin, isLoading } = useAdmin();
if (isLoading) return <Skeleton />;
if (!isAdmin) return <p>Access denied.</p>;
return children;
}
useAdminPermission
Signature
function useAdminPermission(
permission: string
): {
hasPermission: boolean,
isLoading: boolean
}
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
permission | string | Yes | The permission name to check (e.g. "manage_users", "view_reports"). Checked via the admin_has_permission RPC function. |
Return Value
| Property | Type | Description |
|---|---|---|
hasPermission | boolean | true if the current admin has the specified permission. |
isLoading | boolean | true while the permission check is in progress. |
Usage
function DeleteUserButton({ userId }) {
const { hasPermission, isLoading } = useAdminPermission('manage_users');
if (isLoading || !hasPermission) return null;
return <button>Delete User</button>;
}
useSuperAdmin
Signature
function useSuperAdmin(): {
isSuperAdmin: boolean,
isLoading: boolean,
error: string | null
}
Parameters
None. Internally uses useAdmin.
Return Value
| Property | Type | Description |
|---|---|---|
isSuperAdmin | boolean | true if the current user's admin role is exactly "superadmin". |
isLoading | boolean | true while the admin check is in progress. |
error | string | null | Error message if the check failed. |
Usage
function SuperAdminPanel() {
const { isSuperAdmin, isLoading } = useSuperAdmin();
if (isLoading) return <Skeleton />;
if (!isSuperAdmin) return <p>Superadmin access required.</p>;
return <div>Superadmin controls here</div>;
}
Last updated: 2026-02-07