The example code below describes to add authentication to a Next.js app.
To add NextAuth.js to a project create a file called
All requests to
/api/auth/* (signin, callback, signout, etc) will automatically be handed by NextAuth.js.
See the options documentation for how to configure providers, databases and other options.
useSession() React Hook in the NextAuth.js client is the easiest way to check if someone is signed in.
You can use the
useSession hook from anywhere in your application (e.g. in a header component).
To allow session state to be shared between pages - which improves performance, reduces network traffic and avoids component state changes while rendering - you can use the NextAuth.js Provider in
Check out the client documentation to see how you can improve the user experience and page performance by using the NextAuth.js client.
When deploying your site set the
NEXTAUTH_URL environment variable to the canonical URL of the website.