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

@workos-inc/authkit-react

v0.7.2

Published

AuthKit React SDK

Downloads

7,402

Readme

AuthKit React Library

Installation

npm install @workos-inc/authkit-react

or

yarn add @workos-inc/authkit-react

Setup

Add your site's URL to the list of allowed origins in the WorkOS dashboard by clicking on the "Configure CORS" button of the "Authentication" page.

Usage

import { useAuth, AuthKitProvider } from "@workos-inc/authkit-react";

function Root() {
  return (
    <AuthKitProvider clientId="client_123456" apiHostname="auth.example.com">
      <App />
    </AuthKitProvider>
  );
}

function App() {
  const { user, getAccessToken, isLoading, signIn, signUp, signOut } =
    useAuth();

  // This `/login` endpoint should be registered on the "Redirects" page of the
  // WorkOS Dashboard.
  // In a real app, this code would live in a route instead
  // of in the main <App/> component
  React.useEffect(() => {
    if (window.location.pathname === "/login") {
      const searchParams = new URLSearchParams(window.location.search);
      const context = searchParams.get("context") ?? undefined;
      signIn({ context });
    }
  }, [window.location, signIn]);

  if (isLoading) {
    return "Loading...";
  }

  const performMutation = async () => {
    const accessToken = await getAccessToken();
    alert(`API request with accessToken: ${accessToken}`);
  };

  if (user) {
    return (
      <div>
        Hello, {user.email}
        <p>
          <button
            onClick={() => {
              performMutation();
            }}
          >
            Make API Request
          </button>
        </p>
        <p>
          <button onClick={() => signOut()}>Sign out</button>
        </p>
      </div>
    );
  }

  return (
    <>
      <button onClick={() => signIn()}>Sign in</button>{" "}
      <button onClick={() => signUp()}>Sign up</button>
    </>
  );
}

Reference

<AuthKitProvider />

Your app should be wrapped in the AuthKitProvider component. This component takes the following props:

  • clientId (required): Your WORKOS_CLIENT_ID
  • apiHostname: Defaults to api.workos.com. This should be set to your custom Authentication API domain in production.
  • redirectUri: The url that WorkOS will redirect to upon successful authentication. (Used when constructing sign-in/sign-up URLs).
  • devMode: Defaults to true if window.location is "localhost" or "127.0.0.1". Tokens will be stored in localStorage when this prop is true.
  • onRedirectCallback: Called after exchanging the authorization_code. Can be used for things like redirecting to a "return to" path in the OAuth state.

useAuth

The useAuth hook returns user information and helper functions:

  • isLoading: true while user information is being obtained from fetch during initial load.
  • user: The WorkOS User object for this session.
  • getAccessToken: Returns an access token. Will fetch a fresh access token if necessary.
  • signIn: Redirects the user to the Hosted AuthKit sign-in page. Takes an optional state argument.
  • signUp: Redirects the user to the Hosted AuthKit sign-up page. Takes an optional state argument.
  • signOut: Ends the session.
  • switchToOrganization: Switches to the given organization. Redirects to the hosted login page if switch is unsuccessful.

The following claims may be populated if the user is part of an organization:

  • organizationId: The currently-selected organization.
  • role: The role of the user for the current organization.
  • permissions: Permissions corresponding to this role.

Impersonation

Impersonation is not currently supported in authkit-js but is on the roadmap for 2024.