Example
Example project
The easiest way to get started is to clone the example Next.js application from the next-auth-example repository and to the instructions in the README.
You can find a live demo of the example project at next-auth-example.now.sh
Adding NextAuth.js to an existing project
The examples code below shows how to add authentication with NextAuth.js to an existing Next.js project.
Add API route
To add NextAuth.js to a project, first create a file called [...nextauth].js
in pages/api/auth
.
All requests to /api/auth/*
(signin, callback, signout, etc) will automatically be handed by NextAuth.js.
tip
See the options documentation for how to configure providers, databases and other options.
Add React Hook
The useSession()
React Hook in the NextAuth.js client is the easiest way to check if someone is signed in.
That's all the code you need to add authentication to a project!
Configuration
When deploying to production, set the NEXTAUTH_URL
environment variable to the canonical URL of your site.
To set environment variables on Vercel, you can use the dashboard or the now env
command.
tip
Check out the client documentation to see how you can improve the user experience and page performance by using the NextAuth.js client.