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

use-guard-hook

v0.1.0

Published

react permissions-wrapper

Downloads

17

Readme

useGuard Hook

useGuard is designed to be used with React applications where you need to restrict access to specific components based on user permissions. It takes a guard and perms as input and returns a custom React component that acts as a wrapper around its children. The hook checks for specific permissions and renders the wrapped children based on those permissions.

Table of Contents

Installation

  npm install use-guard-hook
  # or
  yarn add use-guard-hook

Description

useGuard is designed to be used with React applications where you need to restrict access to specific components based on user permissions. It takes a guard , permissions, redirect as input and returns a custom React component that acts as a wrapper around its children. The hook checks for specific permit and renders the wrapped children based on those permissions. redirect (optional): renders a React component if access is denied. This allows for seamless redirection to an appropriate UI element when a user lacks the required permissions.

import useGuard from "use-guard-hook";

const permissions = ["read", "write", "delete"];
const guard = "Administrator"; // The Administrator guard name grants all permissions
const redirect = { YourLockComponent }; // redirect(Optional) components if access not granted(null  by default)

const WrappedComponent = useGuard(guard, permissions, redirect);

Usage

The useGuard hook returns a wrapper component that can be used to control access to certain parts of your application. The wrapped component takes a permit prop, which should be a string or a number representing the required permission level to access the content.

<WrappedComponent permit="write">
  {/* Content that requires "write" permission */}
</WrappedComponent>

Permissions

The permissions prop passed to the useGuard hook should be an array of strings or numbers representing the allowed permissions. When checking for permission, the hook converts all elements of the perms array to strings and compares them with the provided permit prop.

Example

Here's a simple example of using the useGuard hook:

import React from "react";
import useGuard from "use-guard-hook";

const permissions = ["read", "write", "delete"]; // Example permissions array
const guard = "Administrator"; // Example guard value (The Administrator guard name grants all permissions)

const App = () => {
  const WrappedComponent = useGuard({ guard, permissions });

  return (
    <div>
      {/* Pass the permissions prop to WrappedComponent */}
      <WrappedComponent permit="write">
        <div>Content that requires "write" permission</div>
      </WrappedComponent>
      <WrappedComponent permit="read">
        <div>Content that requires "read" permission</div>
      </WrappedComponent>
    </div>
  );
};

export default App;

PermissionsProvider

The PermissionsProvider component is used to wrap your app and provide permissions and a guard through the context. It takes the following props: guard: A string representing the user's role (e.g., "Administrator"). permissions: An array of strings or numbers representing the allowed permissions. redirect (optional): A React component to redirect unauthorized users.

import { PermissionsProvider } from "use-guard-hook";
import App from "./App";

const AppWithPermissions = () => (
  <PermissionsProvider
    guard="User"
    permissions={[1, 2, 3]}
    redirect={YourLockComponent}
  >
    <App />
  </PermissionsProvider>
);

useWrapper Hook

The useWrapper hook is used to create a wrapper component that guards access to its children based on permissions and the guard provided by the PermissionsProvider. It takes a permit prop and returns a wrapper component.

import { useWrapper } from "use-guard-hook";

const GuardedComponent = () => {
  const WrappedComponent = useWrapper({ permit: 1 });
  return <WrappedComponent>This content is guarded.</WrappedComponent>;
};

Note

This standalone PermissionsProvider is not intended to work directly with the useGuard hook.