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.
Installation
npminstall--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.
Weβve placed a placeholder publishable API key in this example. Replace it with your actual publishable API key.
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"constMySecurePage=()=>{const{isAuthenticating,isAuthenticated,authState,logout,login}=usePicket(); // user is logging inif (isAuthenticating) return"Loading"; // user is not logged inif (!isAuthenticated) {return ( <div> <p>Youarenotloggedin!</p><buttononClick={() => login()}>LoginwithWallet</button></div> )} // user is logged in πconst{user}=authState;const{walletAddress}=user;return ( <div> <p>Youareloggedinas{walletAddress}</p><buttononClick={() => 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
PicketProvider is a React context provider. It makes the Picket context available throughout your app and easily accessible via the usePicket hook.
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.
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
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.
import { PicketProvider } from "@picketapi/picket-react"
function MyApp({ children }) {
return (
<PicketProvider apiKey="YOUR_PUBLISHABLE_KEY_HERE">
{children}
</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>
);
}
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 π¨";
}
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>
);
}