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
- 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.- Install
wagmiandviemin your React app:
npm2yarn
Initial Setup
First we will add an environment variable that will be used when calling our API.- Create a file called
.envin the root of your react project (wherepackage.jsonis) and add:
wagmi.
- Open
src/App.jsand add our required imports:
- We will add the client and providers, and update the routes for our
/signincomponent (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’sindex.js for the code we need for authentication. In this demo, cookies will be used for the user data.
- Install the required dependencies for our server:
- Create a file called
.envin your server’s root directory (wherepackage.jsonis):
- 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.
- Open
index.js. We will create a/request-messageendpoint for making requests toMoralis.Authto generate a unique message (React will use this endpoint on the/signinpage):
- We will create a
/verifyendpoint for verifying the signed message from the user. After the user successfully verifies, they will be redirected to the/userpage where their info will be displayed:
- We will create an
/authenticateendpoint for checking the JWT cookie we previously set to allow the user access to the/userpage:
- Lastly we will create a
/logoutendpoint for removing the cookie:
index.js should look like this:
Bringing It All Together
Now we will finish setting up our React pages to integrate with our server.- In
src, create a file calledsignin.jsxand add:
- Inside
src, create a new file calleduser.jsxand add:
Testing the MetaMask Wallet Connector
In your teminal runnpm run start and visit http://localhost:3000/signin to test the authentication.
-
Click on the
Authenticate via MetaMaskbutton - Connect the MetaMask wallet and sign the message
-
After successful authentication, you will be redirected to the
/userpage
- When a user authenticates, we show the user’s info on the page.
- When a user is not authenticated, we redirect to the
/signinpage. - When a user is authenticated, we show the user’s info on the page, even refreshing after the page.

