Save a connected wallet to session storage
This is the second part of a tutorial series for building a tokengated storefront. Read the overview and Getting started tutorial before starting this tutorial.
What you'll learn
Anchor link to section titled "What you'll learn"In this tutorial, you'll learn how to do the following tasks:
- Add server-side wallet storage functionality.
- Add an
action
function to yourindex
route for processing dispatched events. - Update your
Layout
component to submit a payload to theindex
route's action when a wallet connects.
Step 1: Create wallet cookie session storage
Anchor link to section titled "Step 1: Create wallet cookie session storage"In this step, you'll add code to create a cookie-based session that stores connected wallet information in the cookie. This information will be used by your storefront's actions and loaders.
For more information on the technology used in this step, reference the links below.
- In your project's
/app/lib
folder, create a new file calledwallet.server.js
, orwallet.server.ts
if you're using TypeScript. Add the following code:
Step 2: Handle wallet connection events
Anchor link to section titled "Step 2: Handle wallet connection events"Next, you will make changes to your ($locale)._index
JSX or TSX file to add an action
function which will run when the onConnect
and onDisconnect
callbacks from @shopify/connect-wallet
.
- Open your
($locale)._index
JSX or TSX file. Copy the following import statements into your file:
Add the following
action
function:
Step 3: Use the index action in connect-wallet callbacks
Anchor link to section titled "Step 3: Use the index action in connect-wallet callbacks"In this step, you'll modify your Layout
component to send the connected wallet data to the action
function that you created in the previous step.
One advantage to storing the wallet in a cookie is that your storefront can load and assess whether a gate is unlocked, or should be, before the client hydration. This means that your customer knows immediately if they meet the criteria for a product if their wallet is already connected.
In your
/app/components/Layout
JSX or TSX file, add the following import statements:Add fetcher and connect-wallet callbacks.
In this step, you'll use the
useFetcher
Remix hook to create a method of submitting data to your index route, anduseConnectWallet
to create the callbacks that run when a wallet is either connected or disconnected.Copy the following code. Add it into your
/app/components/Layout
JSX or TSX file at the top of theLayout
function:
Step 4: Read the context value in a loader
Anchor link to section titled "Step 4: Read the context value in a loader"Next, you'll add additional functionality to your loader
function in the $productHandle
route to retrieve the stored wallet value.
Open your
/app/routes/($locale).products.$productHandle
JSX or TSX fileCopy the following import statements into your file:
Locate the
ProductForm
function and add the<ConnectButton />
component anywhere in the return statement.Copy the following code into the
loader
function:Connect and disconnect your wallet to test out the session storage implementation.
When you connect and disconnect your wallet, you should see the following messages logged to your console.