Add Sign In With Ethereum (SIWE) to a React App

Add Sign In With Ethereum (SIWE) to a React App

Introduction

In this guide, we are going to use Auth to add sign in with Ethereum to our app!

By the end, we'll have a full React application that allows users to connect their wallet and sign in with Ethereum that looks like this:

You can get started with the full code we write in this guide by running:

npx thirdweb create --template sign-in-button

Let's begin!

Why use web3 sign-in?

Sign-in with Ethereum allows you to securely log in using a wallet and verify the wallet on the backend! Our Auth SDK utilizes JSON Web Tokens (JWT) to prove a user owns a wallet in a server-side environment.

Setup

Creating Next.js App

I am going to use the Next javascript starter template for this guide.

If you are following along with the guide, you can create a project with the
Next Javascript template using the thirdweb CLI:

npx thirdweb create --app --next --js

If you already have a Next.js app you can simply follow these steps to get started:

  • Install @thirdweb-dev/react and @thirdweb-dev/sdk and ethers
  • Add MetaMask authentication to the site. You can follow this guide to do this.

Setting up thirdweb auth

Firstly, we need to install the thirdweb auth package:

npm i @thirdweb-dev/auth # npm

yarn add @thirdweb-dev/auth # yarn

Now, create a file called auth.config.js and the following:

import { ThirdwebAuth } from "@thirdweb-dev/auth/next";

export const { ThirdwebAuthHandler, getUser } = ThirdwebAuth({
  // Learn how to securely store your private key below:
  // https://portal.thirdweb.com/sdk/set-up-the-sdk/securing-your-private-key
  privateKey: process.env.PRIVATE_KEY,
  // Set this to be your websites domain name
  domain: "example.org",
});

Learn how we suggest using a secret manager to securely store your private key.

To configure the auth API, create a new folder inside pages/api called auth and a [...thirdweb].js file inside it!

From within this file, we need to export the ThirdwebAuthHandler that we created!

import { ThirdwebAuthHandler } from "../../../auth.config";

export default ThirdwebAuthHandler();

Finally, inside the _app.tsx file, add the authConfig prop to ThirdwebProvider:

  <ThirdwebProvider
      desiredChainId={activeChainId}
      authConfig={{
        authUrl: "/api/auth",
        domain: "example.org",
        loginRedirect: "/",
      }}
    >
      <Component {...pageProps} />
    </ThirdwebProvider>

Building the frontend

Inside pages/index.tsx update the return statement with the following:

return (
    <div>
      <div>
        <ConnectWallet
          auth={{
            loginOptional: false,
          }}
        />
      </div>
      {user && <p>You are signed in as {user.address}</p>}
    </div>
  );

Here, we are using the Thirdweb's ConnectWallet which also has an auth option!

We can also use the useUser hook to get the currently authenticated user:

 import { ConnectWallet, useUser } from "@thirdweb-dev/react";

 const { user } = useUser();

And, it was this easy to add sign in with Ethereum to a dapp with thirdweb!

Conclusion

In this guide, we learned how to use thirdweb auth to add sign in with Ethereum. Share your amazing Dapps that you built using sign in with Ethereum on the thirdweb discord! If you want to take a look at the code, check out the GitHub Repository.