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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-aclify

v0.3.0

Published

react-aclify is a highly useful package that provides a set of tools for Access Control List (ACL) in React applications. It supports TypeScript and presents an intuitive API for managing user roles and permissions.

Downloads

88

Readme

react-aclify

react-aclify is a highly useful package that provides a set of tools for Access Control List (ACL) in React applications. It supports TypeScript and presents an intuitive API for managing user roles and permissions.

Features

  • Role and Permission Management: Utilize React hooks and components to easily manage user roles and permissions for more secure and controlled access.
  • TypeScript Friendly: Full TypeScript support provides strong type checking for user roles, permissions, and more, enhancing the development experience.
  • Granular Access Control: With the CanAccess component, control visibility of parts of your application based on user roles and permissions.
  • Context Interaction: The useAclify hook provides convenient methods (isAuthorized) for interacting with the user's roles and permissions in your context.
  • Adaptable: Thanks to its unopinionated design, react-aclify can be integrated with a variety of project architectures and libraries.

Installation

npm install react-aclify

Usage

Use the createAclify function to create the components and hook for your application:

import { createAclify } from 'react-aclify'

export const { CanAccess, useAclify, AclifyProvider } = createAclify()

For better TypeScript support, you can also pass the Roles and Permissions types to the createAclify function, so that way the components and hook will be typed accordingly:

import { createAclify } from 'react-aclify'

type Role = 'admin' | 'user'
type Permission = 'posts:read' | 'posts:create' | 'posts:delete'

export const { CanAccess, useAclify, AclifyProvider } = createAclify<
  Role,
  Permission
>()

<AclifyProvider />

The AclifyProvider is a context provider that you use to wrap your application, or any part of it where you want to perform role or permission-based actions. The AclifyProvider accepts the following props

  • userRoles: An array of the current user's roles.

  • userPermissions: An array of the current user's permissions.

Here's an example of how you can use the AclifyProvider:

import { AclifyProvider } from '@/lib/aclify'

const App = () => {
  return (
    <AclifyProvider userRoles={['admin']} userPermissions={['read']}>
      {/* Your application goes here */}
    </AclifyProvider>
  )
}

useAclify

The useAclify hook is used to interact with the AclifyProvider context. It returns an object with the following properties:

  • isAuthorized: This function checks whether the user is authorized to access a certain part of the application based on their roles and permissions. It takes an object with roles, permissions, and an optional validationMode, and it returns a boolean indicating whether the user is authorized.

Here's an example of how to use the useAclify hook:

import { useAclify } from '@/lib/aclify'

const Component = () => {
  const { isAuthorized } = useAclify()

  return (
    <div>
      {isAuthorized({ roles: ['user'], permissions: ['posts:read'] }) && (
        <div>Authorized to read posts</div>
      )}
    </div>
  )
}

<CanAccess />

The CanAccess component is a convenient way of restricting certain parts of your UI based on the user's roles and permissions.

It accepts the following props:

  • roles: An array of roles. The user must have at least one of these roles to access the children of the CanAccess component.

  • permissions: An optional array of permissions. If specified, the user must also have these permissions to access the children.

  • validationMode: Optional. Defines how roles and permissions are validated. It accepts:

    • 'all': All roles/permissions must match.
    • 'some': At least one role/permission must match.
  • children: The content that should be rendered if the user has the required roles and permissions.

  • fallback: An optional component or element to be rendered if the user does not have the required roles or permissions.

Here's an example of how to use the CanAccess component:

import { CanAccess } from '@/lib/aclify'

const Component = () => {
  return (
    <div>
      <CanAccess
        roles={['user']}
        permissions={['posts:read']}
        fallback={<div>Not authorized</div>}
      >
        <div>Authorized to read posts</div>
      </CanAccess>
    </div>
  )
}

Contribute

Contributions to react-aclify are always welcome! If you find a bug or have an idea for a new feature, feel free to open an issue or submit a pull request. Every contribution helps make react-aclify a better tool for everyone.

License

MIT License © Gabriel Moraes