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

@contentchef/auth0-react

v0.0.14

Published

A component library for user handling

Downloads

10

Readme

@contentchef/auth0-react

Adds auth0 functionalities in React.

Install

npm i --save @contentchef/auth0-react
# or
yarn add @contentchef/auth0-react

Components

Auth0Provider

Auth0Provider configures your auth0 client in order to perform the right http calls to your auth0 application.

It's highly recommended to read your configuration from an .env file (obviously excluded by the scm) with dotenv package

// in your main application file
import { Auth0Provider } from '@contentchef/auth0-react';
import React from 'react';
import ReactDOM from 'react-dom';
import MyApp from './MyApp';

ReactDOM.render(
  <Auth0Provider
    clientID="your-auth0-client-id"
    domain="your-application-domain"
    redirectUri="/path/to/callback-component"
    responseType="token id_token"
    scope="openid profile email"
  >
    <MyApp />
  </Auth0Provider>
, document.getElementById('app'));

Auth0Callback

Use this component in your callback page(s) set in your auth0 application.

The Auth0Provider component must be an ancestor of this one.

import React from 'react';
import { Auth0Callback } from '@contentchef/auth0-react';

const requestEndHandler = (error, decodedAuthToken) => {
  if (error) {
    /* redirect to an error page */
  }

  /* redirect everywhere you need to */
};

export default () => (
  <Auth0Provider
    autologin={true}
    config={
      {
        clientID: "your-auth0-client-id",
        domain: "your-application-domain",
        redirectUri: "/path/to/callback-component",
        responseType: "token id_token",
        scope: "openid profile email",
      }
    }
    onSessionRenew={session => console.log(session)}
  >
    <Auth0Callback onAuthenticationEnd={requestEndHandler}>
      <h1>You are logging in...</h1>
      <p>You will be redirected in seconds</p>
    </Auth0Callback>
  </Auth0Provider>
)

Authorized/Unauthorized

import React from 'react';
import { Authorized, Unauthorized } from '@contentchef/auth0-react';

export default () => (
  <div>
    <Authorized>
      You are authenticated
    </Authorized>
    <Unauthorized>
      You are not authenticated
    </Unauthorized>
  </div>
)

Login/Logout button

These components can let the user to sign-in / sign-out your auth0 application.

import React from 'react';
import { Authorized, Unauthorized, withUser, IWithUserProps } from '@contentchef/auth0-react';

export default withUser()(({ user }: IWithUserProps) => (
  <div>
    <Authorized>
      Hello { user.name }, <LogoutButton>logout</LogoutButton>
    </Authorized>
    <Unauthorized>
      Hello, <LoginButton>logout</LoginButton>
    </Unauthorized>
  </div>
))

withAuthentication

// in your components/hocs/containers/...
import React from 'react';
import { withAuthentication, LoginButton } from '@contentchef/auth0-react';

export const OnlyAuthUsers = withAuthentication(true)(() => (
  <div>
    <div>
      Hello! You will see this only if authenticated.
    </div>
  </div>
))

export const OnlyUnAuthUsers = withAuthentication(false)(() => (
  <div>
    <div>
      Hello! You are not logged in, please
      <LoginButton>Login</LoginButton> before proceeding
    </div>
  </div>
))

withUser

This decorator will inject a user prop (if authenticated) inside your component props.

// in your components/hocs/containers/...
import React from 'react';
import { Authorized, Unauthorized, withUser, IWithUserProps } from '@contentchef/auth0-react';

export default withUser()(({ user }: IWithUserProps) => (
  <div>
    <Authorized>
      Hello { user.name }, <LogoutButton>logout</LogoutButton>
    </Authorized>
    <Unauthorized>
      Hello, <LoginButton>logout</LoginButton>
    </Unauthorized>
  </div>
));

Examples

import React from 'react';
import Auth0 from '@contentchef/auth0-react';

const UserName = Auth0.withUser()(({ user }) => <span>Hello { user }</span>)

const Application = () => (
  <Auth0.Auth0Provider
    clientID={process.env.AUTH0_CLIENT_ID}
    domain={process.env.AUTH0_DOMAIN}
    redirectUri={process.env.AUTH0_CALLBACK}
    responseType="token id_token"
    scope="openid profile email"
  >
    <header>
      <strong>My App</strong>
      <Auth0.Authorized>
        <UserName />
        <Auth0.Logout>
          <button>Sign out</button>
        </Auth0.Logout>
      </Auth0.Authorized>
      <Auth0.Unauthorized>
        <Auth0.Login>
          <button>Sign in</button>
        </Auth0.Login>
      </Auth0.Unauthorized>
    </header>

    <Auth0.Authorized>
      <section>This is a private section</section>
    </Auth0.Authorized>

  </Auth0.Auth0Provider>
)