πŸ›‘οΈNextAuth.js

A complete open source authentication solution.

NextAuth.jsarrow-up-right is an easy-to-implement, full-stack (client/server) open-source authentication library originally designed for Next.jsarrow-up-right and serverless applications.

The library provides the ability to set up a custom credential provider, which we can take advantage of in order to authenticate users using their existing Ethereum wallet via Sign-In with Ethereum (EIP-4361).

circle-check

Getting started

circle-info

⚑ Requirements

  • First clone the official NextAuth.js example using your terminal:

  • Then, switch to the project directory:

  • After cloning, modify the given .env.local.example file, and populate it with the following variables:

Note: After this, rename the file to .env.local. This example will be routed to http://localhost:3000.

  • Next Add siwe, ethers, and wagmi as dependencies. In this example, we're using wagmiarrow-up-right, which is a well-known React hooks library for Ethereum. In your terminal, navigate to the project we originally cloned and add the dependencies via the following commands:

  • Now, modify pages/_app.tsx to inject the WagmiProvider component:

  • We're going to now add the provider that will handle the message validation. Since it's not possible to sign in using the default page, the original provider should be removed from the list of providers before rendering. Modify pages/api/auth/[...nextauth].ts with the following:

  • The default sign-in page can't be used because there is no way to hook wagmi to listen for clicks on the default sign-in page provided by next-auth, so a custom page must be created to handle the sign-in flow. Create pages/siwe.tsx and populate it with the following:

  • Modify pages/styles.css by appending the following CSS:

Finally, modify the components/header.tsx in order to clean it up and add a SIWE tab to navigate to the newly created page:

  • Run the application using the following commands:

Navigate to localhost:3000 - now you are now ready to Sign-In with Ethereum. Just click the SIWE link in the header, hit the "Sign-In with Ethereum" button, sign the message, and you are now authenticated.

triangle-exclamation

Last updated