Before Starting
You can start this tutorial if you already have a NextJS dapp with MetaMask sign-in functionality.RainbowKit Installation
npm2yarn
RainbowKit Configuration
We are going to modifypages/_app.jsx and add the required code to set up RainbowKit Authentication.
You can get your project ID on the WalletConnect Dashboard.
Authentication with RainbowKit
The logic we’re achieving works as this. A user connects his wallet usingConnectButton from rainbowkit. Once the wallet is connected, we get address and chain from the following wagmi hooks: useAccount() and useNetwork(). In case the user is not authenticated, we will start the authentication flow (request and sign message).
- Open the
pages/signin.jsxfile and replace the oldAuthenticate via MetaMaskbutton with<ConnectButton />from@rainbow-me/rainbowkit:
- Edit
handleAuth()and move it underuseEffect():
- Update missing imports and add new hooks. This is the final code of
pages/signin.jsx:
Set Up RainbowKit with NextJS
The Webpack v5 bundler used by Next.js no longer provides Node polyfills, so you’ll need to include these modules yourself to satisfy RainbowKit’s peer dependencies.
next.config.js file in the root of your project and add the following code:
Testing the RainbowKit Connector
Visithttp://localhost:3000/signin to test authentication.
- Click on
Connect Wallet - Select and connect a wallet you want to use for authentication from the RainbowKit modal
- Sign the message:
- After successful authentication, you will be redirected to the
/userpage - Visit
http://localhost:3000/userto test the user session’s functionality:
- When a user is authenticated, 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. (
Explanation: After Web3 wallet authentication, the next-auth library creates a session cookie with an encrypted JWT [JWE] stored inside. It contains session info [such as an address and signed message] in the user's browser.)

