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-onegraph

v4.0.2

Published

React bindings for OneGraph

Downloads

8

Readme

React Bindings for OneGraph's Authentication Client

Useful React components for working with OneGraph and React. It wraps the OneGraphAuth API automatically rerendering on Authentication changes.

Installation

Note: react-onegraph requires react@^16.3.0 to be installed as a peerDependency.

# yarn
yarn add react-onegraph

# npm
npm i --save react-onegraph

Usage

The package exports 3 parts: AuthProvider, AuthConsumer and AuthContext.


To get started, we have to wrap our application with an AuthProvider. It manages an instance of OneGraphAuth client and passes relevant data using the React Context API.

It takes only the OneGraph appId as props.

import {AuthProvider} from 'react-onegraph';

const APP_ID /* OneGraph appId */ = ReactDOM.render(
  <AuthProvider appId={APP_ID}>
    <App />
  </AuthProvider>,
  document.body,
);

Now one can use the AuthConsumer to get a status per service, request headers and login/logout methods. It implements the render props pattern and automatically updates and rerenders the status and headers on login/logout calls.

The AuthProvider will construct an instance of OneGraphAuth for you. If you want to manage your own instance of OneGraphAuth, you can provide it as the auth prop.

Render Props

| Property | Type |  Description | | -------- | ------------- | ---------------------------------------------------------------------- | | appId | (string) |  The OneGraph appId that was passed to the AuthProvider | | status | (Object) | A map of service-status pairs | | headers | (Object) | The authentication headers object that is used for API requests | | login | (Function) | A function that accepts a service name and an optional status callback | | logout |  (Function) | A function that accepts a service name and an optional status callbac |

import { AuthConsumer } from 'react-onegraph'

const YouTubeAuthentication = (
  <AuthConsumer>
    {({ status, login, logout }) => {
      if (status.youtube) {
        return (
          <div>
            You are logged in!
            <button onClick={() => logout("youtube")}>Logout</button>
          <div>
        )
      }

      return (
        <button onClick={() => login("youtube")}>
          Login
        </button>
      )
    }}
  </AuthConsumer>
)

Callbacks

The login and logout function take an optional second callback parameter. It receives the authentication status for the requested service and is invokes as soon as the login request is resolved.

import { AuthConsumer } from 'react-onegraph'

const YouTubeAuthentication = (
  <AuthConsumer>
    {({ login }) => {
      const loginYoutube = () => login('youtube', () => console.log("Logged in!"))

      return (
        <button onClick={loginYoutube}>
          Login
        </button>
      )
    )}
  </AuthConsumer>
)

Passing the headers

In order to query service data that requires authentication, we need to pass the auth headers to the request.

Apollo

If you're using Apollo there are basically two options to pass the headers. You can either wrap the AuthConsumer around your ApolloProvider and pass the apiId and headers once to the ApolloClient which is passed to the ApolloProvider.

Another option would be to pass the headers to each Query component separately.

import { AuthConsumer } from 'react-onegraph'
import { Query } from 'react-apollo'

const ONEGRAPH_QUERY = /* onegraph gql query */

const OneGraphQuery = (
  <AuthConsumer>
    {({ headers }) => (
      <Query
        query={ONEGRAPH_QUERY}
        context={{ headers }}>
        {({ loading, error, data }) =>  /* render something */}
      </Query>
    )}
  </AuthConsumer>
)

useContext hook

Note: Using hooks requires react @16.7.0-alpha.1 or @16.7.0-alpha.2

import { useContext } from 'react'
import { AuthContext } from 'react-onegraph'

const OneGraphWithHooks = {
  const { status, login, logout, headers, appId } = useContext(AuthContext)

  return (
    // render something
  )
}

License

react-onegraph is licensed under the MIT License. Documentation is licensed under Creative Common License. Created with ♥ by @rofrischmann and all the great contributors.