Next.js
If you want to interact with a working version of the Auth + Next integration that we'll be building in this guide, you can check the following GitHub repository, or clone it with the command below:
npx thirdweb create app --template thirdweb-auth-next
Installation & Setup
Setup your .env
file with the following:
Client-side Setup
Create a lib
directory inside src
and create a client.ts
file inside it:
Add the ThirdwebProvider
in your root layout:
There are two ways to setup Auth on the client-side of a Next.js application, the Connect Button or your own custom component. For both cases, we'll use a JWT in a cookie to store the user's session and authenticate across the app. The connect button will handle a lot of the underlying complexity for you, but if you would like to bring your own components the functions it uses are exposed for your custom use case.
Wherever you'd like your connect/login button, place the ConnectButton
component.
Use this component anywhere in your application. To try it out, we'll put it in the root page.tsx
:
Server-side Setup
We'll use server functions to keep your login secure and concise.
Create an actions
directory with a login.ts
file with the following:
You can include custom context in the JWT token with the context
parameter:
Now anywhere in your project, you can check a user's authentication status: