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

access-permission-control

v0.0.8

Published

The **Access Permission Control** package is a lightweight Role-Based Access Control (RBAC) and Permission-Based Authorization solution written in TypeScript. It is designed to work seamlessly with React applications as well as backend systems (NestJS). T

Readme

Access Permission Control Library Documentation

1. Introduction

The Access Permission Control package is a lightweight Role-Based Access Control (RBAC) and Permission-Based Authorization solution written in TypeScript. It is designed to work seamlessly with React applications as well as backend systems (NestJS). This library helps developers manage user roles, permissions, and access protections in a clean, scalable way.

This documentation covers:

  • Features
  • Architecture
  • File structure
  • Explanation of core concepts
  • How to install and use the package
  • React usage guide
  • API reference
  • Examples (roles, permissions, route guarding)

2. Key Features

2.1 Role Management

  • Assign roles to users (e.g., Admin, Staff, Manager).
  • Check user role via hasRole().

2.2 Permission-Based Access Control

  • Assign permissions such as VIEW DASHBOARD, EDIT USER, etc.
  • Check permissions using hasPermission().

2.3 React Support

  • Comes with a built-in AccessControlProvider.
  • Provides useAccessControl() custom hook.
  • Can be used to protect components, pages, or routes.

2.4 TypeScript Support

  • Full type definitions included.

2.5 NestJS Support (Optional)

  • Works with backend permission validation.

3. Installation

Install from npm:

npm install access-permission-control

4. Package Structure

access-permission-control/
│
├── src/
│   ├── react/
│   │   ├── AccessControlProvider.tsx
│   │   ├── RouteGuard.tsx
│   │   └── index.ts
│   │
│   ├── nestjs/
│   │   └── decorators, guards, utils...
│   │
│   ├── index.react.ts
│   ├── index.nestjs.ts
│
├── dist/
│   ├── react/* (bundled files)
│   └── nestjs/* (bundled files)
│
└── package.json

5. Core Concepts

5.1 Roles

Roles are high‑level groups like:

  • Admin
  • Manager
  • User

Example:

roles: ["Admin"]

5.2 Permissions

Permissions define exactly what a user can do:

permissions: ["VIEW DASHBOARD", "EDIT DOCUMENT"]

5.3 AccessControlProvider

A React context provider that exposes:

  • roles
  • permissions
  • hasRole(role)
  • hasPermission(permission)

5.4 useAccessControl() Hook

Allows any component to ask:

  • Does user have a role?
  • Does user have a permission?

6. How to Use (React)

6.1 Wrap Your App with Permission Provider

'use client';
import PermissionProvide from './PermissionProvide';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <PermissionProvide>{children}</PermissionProvide>
      </body>
    </html>
  );
}

7. PermissionProvide Component

This component loads user data from localStorage and passes it to the RBAC system.

Example Implementation

'use client';

import { ReactNode, useEffect, useState } from 'react';
import { AccessControlProvider } from 'access-permission-control/dist/index.react';

export default function PermissionProvide({ children }) {
  const [roles, setRoles] = useState([]);
  const [permissions, setPermissions] = useState([]);
  const [isReady, setIsReady] = useState(false);

  useEffect(() => {
    const userString = localStorage.getItem('currentUser');
    if (userString) {
      const currentUser = JSON.parse(userString);
      setRoles([currentUser.role]);
      setPermissions(currentUser.permissions);
    }
    setIsReady(true);
  }, []);

  if (!isReady) return null;

  return (
    <AccessControlProvider roles={roles} permissions={permissions}>
      {children}
    </AccessControlProvider>
  );
}

8. Using the useAccessControl Hook

Example inside a React component:

'use client';
import { useAccessControl } from 'access-permission-control/dist/index.react';

export default function DashboardPage() {
  const { hasRole, hasPermission } = useAccessControl();

  return (
    <>
      {hasRole('Admin') && <h1>Welcome Admin</h1>}
      {hasPermission('VIEW DASHBOARD') ? <p>You can view dashboard</p> : <p>Access denied</p>}
    </>
  );
}

9. Using RouteGuard

import { RouteGuard } from 'access-permission-control/dist/index.react';

<RouteGuard requiredRoles={["Admin"]}>
  <AdminSettingsPage />
</RouteGuard>

10. How to Store User After Login

localStorage.setItem(
  'currentUser',
  JSON.stringify({
    role: 'Admin',
    permissions: ['VIEW DASHBOARD', 'MANAGE USERS'],
  })
);

11. Backend / NestJS Support

The nestjs build includes decorators and guards for role/permission enforcement.

Example:

@UseGuards(PermissionGuard('EDIT_USER'))
@Get('users')
findAllUsers() {}

12. Summary

The access-permission-control library provides:

  • A clean way to manage RBAC
  • Permission-based UI protection
  • React provider + hook for easy integration
  • NestJS backend decorators

This solves:

  • Hiding UI for unauthorized roles
  • Preventing unauthorized access
  • Consistent permission management across frontend and backend

If you want a PDF export, README.md, or API diagram, I can generate that next.