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

@adrianfrith/react-u5auth

v0.2.1

Published

React components to authenticate via U5auth (OAuth2)

Downloads

6

Readme

What is It?

A simple React component and higher order function to facilitate authentication via U5-Auth (or rather, any OAuth2 provider).

Prerequisites

  • The provider url, e.g. https://login.u5auth.com.
  • OAuth2 client credentials, where the client is allowed to use the implicit flow.
  • A React application, which is supposed to be secured via OAuth2 (or rather, needs an access_token to authenticate e.g. calls to APIs).

How

Setup

Client details need to be specified via the AuthContext component. The AuthContext must wrap any component that needs authentication, so the AuthContext is probably best placed high up in the component tree (maybe just above the router, if you use one):

First, import the AuthContext component:

import { AuthContext } from 'react-u5auth'

Then, wrap your component(s):

<AuthContext clientId={"123"} provider={"https://my-provider.com"}>
  <Router history={browserHistory}>
    // ...
  </Router>
</AuthContext>

Protecting Components

Now, the higher-order function authenticated can be used to ensure a valid access_token whenever the component gets rendered:

import { authenticated } from 'react-u5auth'

class SomeComponent extends React.Component {
  render() {
    return <p>Some component that needs an authenticated user...</p>
  }
}

const ProtectedComponent = authenticated()(SomeComponent)

const SomeOtherComponent = () => (<p>This is some other component</p>)
const ProtectedComponent = authenticated()(() => (<SomeOtherComponent />))

Instead of the higher-order function the hook useAuthentication can be used for the same purpose:

import { useAuthentication } from 'react-u5auth'

const SomeComponent = () => (
  <p>Some component that needs an authenticated user...</p>
)

const ProtectedComponent = () => {
  const { authenticated } = useAuthentication()

  if (!authenticated) {
    // This will appear only for a short time before the
    // redirect to the auth provider URL kicks in.
    return <p>Logging in...</p>
  } else {
    return <SomeComponent />
  }
}

Using the access_token

A protected component isn't too valuable on its own, you may need an access token when speaking to an API. Anywhere the access token can be accessed like this:

import { getLocalToken } from 'react-u5auth'

...
const token = getLocalToken()
...

The useAuthentication hook also returns the token:

const { authenticated, token } = useAuthentication()

Please note: There is something fishy here about the access_token being kept in global state. See this issue.

Status

It's fully functional, but does not deal with token expiry and/or certain error conditions yet. See the issues and/or add a new issue.