Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@postinumero/authorization

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@postinumero/authorization

This package needs to be transpiled. Add this to Babel configuration:

  • 0.3.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@postinumero/authorization

This package needs to be transpiled. Add this to Babel configuration:

{
  "babelrcRoots": ["./node_modules/@postinumero/**"]
}

API

withAuthorization(Component)

Wrap the application with authorization context. <Authenticated>, <Authorize>, useUser and useSetUser are available in the wrapped component and children.

import { withAuthorization } from "@postinumero/authorization";
// …
withAuthorization(App);

<Authenticated>

Display the content only for authenticated users. Optionally render a fallback UI for public users.

fallback: element (optional)

import { Authenticated } from "@postinumero/authorization";
// …
<Authenticated fallback={<Public />}>
  <Private />
</Authenticated>;

<Authorize>

Display the content only for certain user roles. Optionally render a fallback UI for unauthorized users.

allow: string|string[]

fallback: element (optional)

import { Authorize } from "@postinumero/authorization";
// …
<Authorize allow="admin" fallback={<Forbidden />}>
  <h1>Admin</h1>
</Authorize>;

useUser()

Get user.

import { useUser } from "@postinumero/authorization";

function UserInfo() {
  const user = useUser();
  <div>{user.name}</div>;
}

useSetUser(user)

Set user.

import { useSetUser } from "@postinumero/authorization";

function Login() {
  const setUser = useSetUser();
  return (
    <button
      onClick={() => {
        // Get user somehow
        const user = { id: "1", name: "Admin", roles: ["admin"] };
        setUser(user);
      }}
    >
      Login
    </button>
  );
}

useAuthorize({ allow: string|string[] })

Hook to detect if user has any of allowed roles. Internally used by <Authorize>.

import { useAuthorize } from "@postinumero/authorization";
// …
const isAdmin = useAuthorize({ allow: "admin" });

FAQs

Package last updated on 17 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc