Skip to main content

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 role
  • useAdminPermission -- checks if the admin has a specific named permission
  • useSuperAdmin -- 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

PropertyTypeDescription
isAdminbooleantrue if the current user has any admin role.
adminRolestring | nullThe user's admin role string (e.g. "admin", "superadmin", "moderator"), or null if not an admin.
isLoadingbooleantrue while the admin check is in progress.
errorstring | nullError 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

ParameterTypeRequiredDescription
permissionstringYesThe permission name to check (e.g. "manage_users", "view_reports"). Checked via the admin_has_permission RPC function.

Return Value

PropertyTypeDescription
hasPermissionbooleantrue if the current admin has the specified permission.
isLoadingbooleantrue 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

PropertyTypeDescription
isSuperAdminbooleantrue if the current user's admin role is exactly "superadmin".
isLoadingbooleantrue while the admin check is in progress.
errorstring | nullError 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