Picket Docs
  • ๐Ÿ‘‹Welcome to Picket
  • Quick Start Guides
    • ๐Ÿš€Quick Start Guides
      • โš™๏ธStart Here: Setup
      • ๐Ÿ”Wallet Login
      • ๐Ÿช™Token Gating (Ethereum / EVM)
      • ๐ŸคบToken Gating (Solana)
      • ๐Ÿคนโ€โ™‚๏ธIncremental Token Gating
      • โ›”Restrict Access to Specific Wallets
      • ๐Ÿ”‘Working with Access Tokens
  • Reference
    • ๐ŸŽ“Concepts
      • ๐ŸŒŠAuth Flow
      • ๐Ÿ—ƒ๏ธConnect
      • โœ๏ธSignatures
      • ๐Ÿ”Authentication and Authorization
      • ๐ŸคนIncremental Authorization
      • ๐Ÿช™Access Tokens
      • ๐ŸงชTesting
      • โ‰๏ธErrors
      • โ›“๏ธSupported Blockchains
      • ๐ŸŒSupported Languages (Localization)
      • ๐ŸŽจModal Themes
      • ๐Ÿ’ฟOpen Source Web3 Client Libraries
    • ๐Ÿ“šLibraries and SDKs
      • Javascript Library - picket-js
      • React SDK - picket-react
      • Node.js Library - picket-node
      • Go Library - picket-go
      • Python Library - picket-python
    • ๐Ÿ”ฅIntegrations
      • ๐ŸŒˆPicket Authentication with RainbowKit
      • โšกSupabase
      • โ˜๏ธAmazon Cognito
      • ๐Ÿ›๏ธPicket Shopify App - Merchant Documentation
      • ๐Ÿ›’Picket BigCommerce App - Merchant Documentation
    • ๐Ÿ“–API Reference
      • Projects & API Keys
      • Auth
      • Chains
      • Wallets
      • Contracts
      • OAuth 2.0
  • ๐Ÿ•น๏ธTutorials
    • ๐ŸŒŽSign-In with Wallet (React)
    • ๐ŸฐToken Gated Photo Board (React)
    • ๐Ÿ”—Link a Wallet to a Web 2.0 Account
    • ๐ŸคIncremental Authorization (React)
Powered by GitBook
On this page
  • Installation
  • Usage - Quick Start
  • PicketProvider
  • usePicket
  • PicketRainbowAuthProvider
  1. Reference
  2. Libraries and SDKs

React SDK - picket-react

The Picket React SDK, picket-react, is a JavaScript library for integrating Picket into React apps. It gives access to a Picket context provider and custom hook for securing your app.

PreviousJavascript Library - picket-jsNextNode.js Library - picket-node

Last updated 2 years ago

Installation

npm install --save "@picketapi/picket-react"

Usage - Quick Start

The PicketProvider creates a Picket context, which makes user authentication information available throughout your app! It takes a publishable API key as a prop.

import { PicketProvider } from "@picketapi/picket-react"

function MyApp({ children }) {
  return (
    <PicketProvider apiKey="YOUR_PUBLISHABLE_KEY_HERE">
      {children}
    </PicketProvider>
  );
}

Weโ€™ve placed a placeholder publishable API key in this example. Replace it with your .

After instantiating the PicketProvider, you can use the usePicket hook to get user authentication information within your app. Below is an example of a component that renders different information based on the user's authentication state.

import { usePicket } from "@picketapi/picket-react"

const MySecurePage = () => {
  const { 
          isAuthenticating, 
          isAuthenticated, 
          authState, 
          logout,
          login
          } = usePicket();
  
  // user is logging in
  if (isAuthenticating) return "Loading";

  // user is not logged in
  if (!isAuthenticated) {
      return (
        <div>
            <p>You are not logged in!</p>
            <button onClick={() => login()}>Login with Wallet</button>
        </div>
      )
  }

  // user is logged in ๐ŸŽ‰
  const { user } = authState;
  const { walletAddress } = user;
  
  return (
    <div>
       <p>You are logged in as {walletAddress} </p>
       <button onClick={() => logout()}>Logout</button>
    </div>
  )
}

The usePicket hook provides your components information about the user's authentication state. You can use it to require authentication on specific routes, get user information, or get the login and logout functions.

PicketProvider

It's typical to wrap the outer-most component with the PicketProvider to make a user's authentication and authorization state globally available to all child components.

import { PicketProvider } from "@picketapi/picket-react"

function MyApp({ children }) {
  return (
    <PicketProvider apiKey="YOUR_PUBLISHABLE_KEY_HERE">
      {children}
    </PicketProvider>
  );
}

Themes

The Picket Login Modal supports several themes. By default the login modal will use the light theme. However, you can set it to a different theme to best fit into your overall web experience.

You can set the theme by passing a prop to the PicketProvider

import { PicketProvider } from "@picketapi/picket-react"

function MyApp({ children }) {
  return (
    {/* sets theme to dark */}
    <PicketProvider apiKey="YOUR_PUBLISHABLE_KEY_HERE" theme="dark">
      {children}
    </PicketProvider>
  );
}

usePicket

usePicket is a wrapper around the useContext React hook that makes all Picket-related authentication information accessible.

import { usePicket } from "@picketapi/picket-react"

const MySecureComponent = () => {
  const { 
          isAuthenticating, 
          isAuthenticated,
          authState, 
          logout,
          login
          } = usePicket();
  // do something with the user's authentication state
  // redirect not logged in
  // make API requests with access token
  // display wallet address
  // etc
  return isAuthenticated ? "you've logged in ๐Ÿ˜Ž" : "you need to login ๐Ÿšจ";
}
Name
Type
Description

isAuthenticating

bool

True if the login process has started and hasn't finished or Picket is validating the cached access token on initialization

isAuthenticated

bool

True if the user has recently logged in and there is a cached, valid access token

isAuthorized

function

isAlreadyAuthorized

function

Check if the current user has already been authorized. This is synchronous, local-only check. I is useful for showing different states for UI elements.

authState

AuthState | null

The auth state contains the data from the last successful login.

null if the user is not authenticated

error

Error

The latest error, if any, when authenticating a user.

error is helpful for knowing when to display error messages to users.

login

function

logout

function

loginWithRedirect

function

loginWithPopup

function

PicketRainbowAuthProvider

import { PicketRainbowAuthProvider } from "@picketapi/picket-react";

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <WagmiConfig client={wagmiClient}>
      {/* 
        1. ORDER MATTERS! PicketRainbowAuthProvider must be nested 
           between WagmiConfig and RainbowKitProvider
        2. Replace YOUR_PUBLISHABLE_KEY_HERE with your project's publishable key
      */}
      <PicketRainbowAuthProvider apiKey="YOUR_PUBLISHABLE_KEY_HERE">
        <RainbowKitProvider chains={chains}>
          <Component {...pageProps} />
        </RainbowKitProvider>
      </PicketRainbowAuthProvider>
    </WagmiConfig>
  );
}

PicketProvider is a . It makes the Picket context available throughout your app and easily accessible via the .

Supported themes can be found .

Given authorization requirements, check if the current user is authorized. This is equivalent to

The

The

The function

function

PicketRainbowAuthProvider allows you to use Picket for authentication and authorization with the modal.

Interested in learning more? Go to the integration page.

๐Ÿ“š
actual publishable API key
here
RainbowKit
Picket Authentication with RainbowKit
React context provider
usePicket hook
isCurrentUserAuthorized in picket-js
picket-js login function
picket-js logout function
picket-js loginWithRedirect
The picket-js loginWithPopup