npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-acl-service

v1.0.0

Published

Backend-agnostic Access Control List (ACL) system for React with role-based permissions

Downloads

89

Readme

react-acl-service

A backend-agnostic, type-safe Access Control List (ACL) system for React applications. Designed for real production apps with role-based permissions, route protection, and UI-level access control.

⚠️ This library controls UI visibility only. Backend APIs must always enforce authorization separately.


✨ Features

  • 🔐 Role-based access control (RBAC)
  • ⚡ Async permission loading from backend
  • 🧠 TypeScript-first public API
  • 🧩 Backend-agnostic (REST, GraphQL, RPC, etc.)
  • 🚦 Route-level protection (documented, not coupled)
  • 🧱 Page / module hiding
  • 🎯 Button & element-level access control
  • 🔄 Role switching & permission refresh
  • 💾 Optional role persistence

📦 Installation

npm install react-acl-service

🧠 Core Concepts

Permission Model

Permissions are evaluated using moduleKey + permissionKey pairs.

Example:

USER.CREATE
USER.VIEW
ROLE.MANAGE

Your backend should return permissions in this shape:

ModulePermissionPair

🚀 Quick Start

1️⃣ Wrap your app with ACLProvider

import { ACLProvider } from 'react-acl-service';

const getUser = () => ({
  roleId: 1,
  roleName: 'ADMIN',
});

const fetchAllPermissions = async () => [
  {
    moduleId: 1,
    moduleKey: 'USER',
    permissionId: 1,
    permissionKey: 'CREATE',
  },
  {
    moduleId: 1,
    moduleKey: 'USER',
    permissionId: 2,
    permissionKey: 'VIEW',
  },
];

const fetchRolePermissions = async (roleId: number) => [
  {
    moduleId: 1,
    moduleKey: 'USER',
    permissionId: 1,
    permissionKey: 'CREATE',
  },
];

function App() {
  return (
    <ACLProvider
      getUser={getUser}
      fetchAllPermissions={fetchAllPermissions}
      fetchRolePermissions={fetchRolePermissions}
    >
      <Dashboard />
    </ACLProvider>
  );
}

🧩 Conditional Rendering with ACLGate

Use ACLGate to show or hide UI elements.

import { ACLGate } from 'react-acl-service';

<ACLGate
  moduleKey="USER"
  permissionKey="CREATE"
  fallback={<p>No access</p>}
>
  <button>Create User</button>
</ACLGate>

Behavior

  • Permission granted → children rendered
  • Permission denied → fallback OR hidden

🚫 Hide vs Disable Elements

🔹 Hide element completely (default)

<ACLGate moduleKey="USER" permissionKey="DELETE">
  <button>Delete</button>
</ACLGate>

🔹 Disable element but keep visible

import { useACL } from 'react-acl-service';

const DeleteButton = () => {
  const { hasPermission } = useACL();
  const allowed = hasPermission('USER', 'DELETE');

  return (
    <button disabled={!allowed}>
      Delete
    </button>
  );
};

🧱 Full Page / Module Hiding

Use ACL to remove entire pages or modules from UI.

Example – Sidebar Menu

<ACLGate moduleKey="USER" permissionKey="VIEW">
  <NavLink to="/users">Users</NavLink>
</ACLGate>

If permission is missing → menu item is not rendered at all.


🚦 Route Protection (Page-Level)

This library is router-agnostic. Route protection is implemented in your app, using useACL.

Example (React Router)

import { Navigate } from 'react-router-dom';
import { useACL } from 'react-acl-service';

const ProtectedRoute = ({ children }) => {
  const { hasPermission, loading } = useACL();

  if (loading) return null;

  if (!hasPermission('USER', 'VIEW')) {
    return <Navigate to="/unauthorized" replace />;
  }

  return children;
};

⚠️ Route guards are not included in the package by design.


🧭 Navigation / Tabs / Sections

Tabs Example

import { useACL } from 'react-acl-service';

const Tabs = () => {
  const { hasPermission } = useACL();

  return (
    <>
      {hasPermission('USER', 'VIEW') && <Tab label="Users" />}
      {hasPermission('ROLE', 'VIEW') && <Tab label="Roles" />}
    </>
  );
};

🪝 Using the useACL Hook

import { useACL } from 'react-acl-service';

const Dashboard = () => {
  const {
    currentRole,
    hasPermission,
    refreshPermissions,
  } = useACL();

  return (
    <>
      <p>Role: {currentRole?.name}</p>

      {hasPermission('USER', 'CREATE') && (
        <button>Create User</button>
      )}

      <button onClick={refreshPermissions}>
        Refresh Permissions
      </button>
    </>
  );
};

🧠 Permission Utilities

import {
  checkPermission,
  checkAnyPermission,
  checkAllPermissions,
} from 'react-acl-service';

Any Permission Required

checkAnyPermission(permissions, [
  { moduleKey: 'USER', permissionKey: 'CREATE' },
  { moduleKey: 'USER', permissionKey: 'UPDATE' },
]);

All Permissions Required

checkAllPermissions(permissions, [
  { moduleKey: 'USER', permissionKey: 'CREATE' },
  { moduleKey: 'USER', permissionKey: 'APPROVE' },
]);

🧾 TypeScript Support

Exported Types

import type {
  ModulePermissionPair,
  ACLProviderProps,
} from 'react-acl-service';

⚙️ ACLProvider Props

| Prop | Required | Description | | ---------------------- | -------- | ---------------------------- | | getUser | ✅ | Returns logged-in user role | | fetchAllPermissions | ❌ | Fetches all permissions | | fetchRolePermissions | ❌ | Fetches role permissions | | initialPermissions | ❌ | Static permissions | | persistRole | ❌ | Persist role in localStorage | | storageKey | ❌ | Custom storage key |


🏗️ Recommended Architecture

Backend → returns permissions
Frontend App → passes APIs
react-acl-service → evaluates access

✔ No backend coupling ✔ No routing assumptions ✔ Fully scalable


🔐 Security Note (IMPORTANT)

This library does not secure APIs. Backend must always validate permissions.

ACL is for UI visibility & UX, not enforcement.


📄 License

MIT © Anantha Lakshmi Gatta