Skip to main content

Introduction

This tutorial demonstrates how to create a React app that allows users to log in using their Web3 wallets. After Web3 wallet authentication, the server creates a session cookie with a signed JWT stored inside. It contains session info (such as an address, signed message) in the user’s browser. Once the user is logged in, they will be able to visit a page that displays all their user data.

Prerequisites

  1. Follow the Your First Dapp - React tutorial to set up your React dapp and server

Install the Required Dependencies

To implement authentication using a Web3 wallet (e.g., MetaMask), we will use a Web3 library. For the tutorial, we will use wagmi.
  1. Install wagmi and viem in your React app:
npm2yarn

Initial Setup

First we will add an environment variable that will be used when calling our API.
  1. Create a file called .env in the root of your react project (where package.json is) and add:
Next we will add the providers required for wagmi.
  1. Open src/App.js and add our required imports:
  1. We will add the client and providers, and update the routes for our /signin component (to be set up next):

Your full App.js file should look like this

Server Setup

Back in our server directory we will update our server’s index.js for the code we need for authentication. In this demo, cookies will be used for the user data.
  1. Install the required dependencies for our server:
  1. Create a file called .env in your server’s root directory (where package.json is):
  • APP_DOMAIN: RFC 4501 DNS authority that is requesting the signing.
  • MORALIS_API_KEY: You can get it here.
  • REACT_URL: Your app address. By default React uses http://localhost:3000.
  • AUTH_SECRET: Used for signing JWT tokens of users. You can put any value here or generate it on https://generate-secret.now.sh/32.
  1. Open index.js. We will create a /request-message endpoint for making requests to Moralis.Auth to generate a unique message (React will use this endpoint on the /signin page):
  1. We will create a /verify endpoint for verifying the signed message from the user. After the user successfully verifies, they will be redirected to the /user page where their info will be displayed:
  1. We will create an /authenticate endpoint for checking the JWT cookie we previously set to allow the user access to the /user page:
  1. Lastly we will create a /logout endpoint for removing the cookie:
Your final index.js should look like this:

Bringing It All Together

Now we will finish setting up our React pages to integrate with our server.
  1. In src, create a file called signin.jsx and add:
  1. Inside src, create a new file called user.jsx and add:

Testing the MetaMask Wallet Connector

In your teminal run npm run start and visit http://localhost:3000/signin to test the authentication.
  1. Click on the Authenticate via MetaMask button
  2. Connect the MetaMask wallet and sign the message
  3. After successful authentication, you will be redirected to the /user page
  • When a user authenticates, we show the user’s info on the page.
  • When a user is not authenticated, we redirect to the /signin page.
  • When a user is authenticated, we show the user’s info on the page, even refreshing after the page.