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-hall-pass

v0.2.0

Published

A small React library to help with user permissions

Downloads

3

Readme

react-hall-pass

A small React library to help with user permissions. This was largely inspired by react-lever, a small library to help with feature flags.

Install

with yarn:

yarn add react-hall-pass

or without yarn:

npm install --save react-hall-pass

Usage

Example site

Using the HallPass component

import * as React from "react";

import { HallPass } from "react-hall-pass";

class Example extends React.Component {
  const user = {
    name: "example user",
    permissions: ["PERMISSION_1", "PERMISSION_2"]
  }
  render() {
    return (
      <>
        <h1>Quick HallPass Example</h1>

        <p>public content for anybody to see</p>

        <HallPass
          requiredPermissions={["PERMISSION_1"]}
          userPermissions={user.permissions}
        >
          <p>
            some content that should only be seen by those with the proper permissions (in this case, users that have the "PERMISSION_1" permission)
          </p>
        </HallPass>
      </>
    );
  }
}

Using the hook directly

this is what the <HallPass> component uses under the hood

import * as React from "react";
import { useHallPass } from "react-hall-pass";

const OtherExample: React.FC = () => {
  const user = {
    name: "example user",
    permissions: ["PERMISSION_1", "PERMISSION_2"]
  }

  // const passesChecks = useHallPass(userPermissions, requiredPermissions, exceptions?);
  const passesChecks = useHallPass(user.permissions, ["PERMISSION_1"]);

  if (!passesChecks) {
    return <p>you don't have the correct permissions</p>
  }
  return (
    <div>
      the user has the permissions required to view this content
    </div>
  )
}

parameters

| param | type | required | | ------------------- | ------------------------------------------ | -------- | | userPermissions | Array<string> | string | true | | requiredPermissions | Array<string> | string | true | | exceptions | Array<string> | string | false |

NOTE - currently no parameters have defaults

Note about exceptions

Be careful with the exceptions prop as it is powerful and can/will override the default behavior of strictly checking the userPermissions against the requiredPermissions. So in general, try not to use it unless you actually need it for an exception to the norm. It is mainly meant to be an available/accessible escape route.

About

I started working on this after looking at the afformentioned react-lever library that helps with feature flags. A few weeks into it, I realized that I hadn't really scoped out the landscape of libraries for permissions in react, but decided to continue making this after seeing a few that already existed.

A few goals I had:

  • render inside children (pass whatever you want to the HallPass component) - this is the "default" behavior
  • expose a hook that allows the developer to use the hook directly and not have to use the HallPass component
  • write the library in TypeScript
  • make sure that HallPass wasn't too opionated with regards to where it was used. I've seen a few libraries that make some assumptions on if you're using a certain state management library (ie. redux) or not, etc. (React-Hall-Pass is of course opionated by the fact that you have to pass an array (or string) and not some other custom data structure (though I guess that could change if it could be implemented well enough))
  • make sure the library was well documented (I'm a big fan of well documented things and learning from examples, so I'd like to document this library well and have a decent amount of examples for potential users of the library)

Also, this is one of the first things I've worked on in TypeScript, so feel free to help out if anything seems a bit off.

Contributing

Thoughts/ideas/problems/fixes welcome. Please create an issue and we can discuss.

License

MIT © nickbouldien