Introduction
This tutorial shows you how to create a NextJS application that lets users log in using their Phantom wallet. 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, signed message, and expiration time) in the user’s browser. It’s a secure way to store users’ info without a database, and it’s impossible to read/modify the JWT without a secret key. Once the user is logged in, they will be able to visit a page that displays all their user data.You can find the final dapp with implemented style on our GitHub.
Prerequisites
- Create a Moralis account.
- Install and set up Visual Studio.
- Create your NextJS dapp (you can create it using create-next-app or follow the NextJS dapp tutorial).
Install the Required Dependencies
- Install
@moralisweb3/next(if not installed),next-authand@web3uikit/coredependencies:
npm2yarn
- To process data like the signature of a Solana Web3 wallet (e.g., Phantom), we need the
bs58package to encode and decode data from the wallet. Let’s install thebs58package:
npm2yarn
- Add new environment variables in your
.env.localfile in the app root:
- APP_DOMAIN: RFC 4501 DNS authority that is requesting the signing.
- MORALIS_API_KEY: You can get it here.
- NEXTAUTH_URL: Your app address. In the development stage, use
http://localhost:3000. - NEXTAUTH_SECRET: Used for encrypting JWT tokens of users. You can put any value here or generate it on
https://generate-secret.now.sh/32. Here’s an.env.localexample:
.env.local
Keep your
Every time you modify the
NEXTAUTH_SECRET value in secret to prevent security problems.Every time you modify the
.env.local file, you need to restart your dapp.Wrapping App with SessionProvider
- Create the
pages/_app.jsxfile. We need to wrap our pages withSessionProvider(docs):
NextJS uses the
App component to initialize pages. You can override it and control the page initialization. Check out the NextJS docs.Configure Next-Auth and MoralisNextAuth
- Create a new file,
pages/api/auth/[...nextauth].ts, with the following content:
- Tab
- Tab
- Add an authenticating config to the
pages/api/moralis/[...moralis].ts:
- Tab
- Tab
[...moralis].ts
Create Wallet Component
- Create a new file under
app/components/loginBtn/phantomBtn.tsx:
- Tab
- Tab
phantomBtn.tsx
Create Page to Sign-In
- Create a new page file,
pages/index.jsx, with the following content:
- You can get the app CSS from GitHub to style the app.
Logout and User Profile Component
- Create components to perform the logout operation and to show the user data.
- Tab
- Tab
logoutBtn.js
Showing the User Profile
- Let’s create a
user.jsxpage to view user data when the user is logged in.
Testing with Phantom Wallet
Visithttp://localhost:3000 to test the authentication.
- Click on the
Select Walletbutton to select and connect to wallet - Connect to the Solana wallet extension
- Sign the message
- After successful authentication, you will be redirected to the
/userpage

