NextAuth.js

Authentication for Next.js

README

undefined

NextAuth.js


Authentication for Next.js

Open Source. Full Stack. Own Your Data.

Release Bundle Size Downloads Github Stars Github Stable Release

Overview


NextAuth.js is a complete open source authentication solution for Next.js applications.

It is designed from the ground up to support Next.js and Serverless.

This is a monorepo containing the following packages / projects:

The primary next-authpackage
A development test application
All @next-auth/*-adapterpackages
The documentation site

Getting Started


  1. ``` null
  2. npm install --save next-auth

  3. ```

The easiest way to continue getting started, is to follow the getting started section in our docs.

We also have a section of tutorials for those looking for more specific examples.

See next-auth.js.org for more information and documentation.

Features


Flexible and easy to use


Designed to work with any OAuth service, it supports OAuth 1.0, 1.0A and 2.0
Built-in support for many popular sign-in services
Supports email / passwordless authentication
Supports stateless authentication with any backend (Active Directory, LDAP, etc)
Supports both JSON Web Tokens and database sessions
Designed for Serverless but runs anywhere (AWS Lambda, Docker, Heroku, etc…)

Own your own data


NextAuth.js can be used with or without a database.

An open source solution that allows you to keep control of your data
Supports Bring Your Own Database (BYOD) and can be used with any database
Works great with databases from popular hosting providers
Can also be used without a database(e.g. OAuth + JWT)

Secure by default


Promotes the use of passwordless sign-in mechanisms
Designed to be secure by default and encourage best practices for safeguarding user data
Uses Cross-Site Request Forgery (CSRF) Tokens on POST routes (sign in, sign out)
Default cookie policy aims for the most restrictive policy appropriate for each cookie
When JSON Web Tokens are enabled, they are encrypted by default (JWE) with A256GCM
Auto-generates symmetric signing and encryption keys for developer convenience
Features tab/window syncing and session polling to support short lived sessions
Attempts to implement the latest guidance published by Open Web Application Security Project

Advanced options allow you to define your own routines to handle controlling what accounts are allowed to sign in, for encoding and decoding JSON Web Tokens and to set custom cookie security policies and session properties, so you can control who is able to sign in and how often sessions have to be re-validated.

TypeScript


NextAuth.js comes with built-in types. For more information and usage, check out the TypeScript section in the documentation.

Example


Add API Route


  1. ``` js
  2. // pages/api/auth/[...nextauth].js
  3. import NextAuth from "next-auth"
  4. import AppleProvider from "next-auth/providers/apple"
  5. import GoogleProvider from "next-auth/providers/google"
  6. import EmailProvider from "next-auth/providers/email"

  7. export default NextAuth({
  8.   secret: process.env.SECRET,
  9.   providers: [
  10.     // OAuth authentication providers
  11.     AppleProvider({
  12.       clientId: process.env.APPLE_ID,
  13.       clientSecret: process.env.APPLE_SECRET,
  14.     }),
  15.     GoogleProvider({
  16.       clientId: process.env.GOOGLE_ID,
  17.       clientSecret: process.env.GOOGLE_SECRET,
  18.     }),
  19.     // Sign in with passwordless email link
  20.     EmailProvider({
  21.       server: process.env.MAIL_SERVER,
  22.       from: "<no-reply@example.com>",
  23.     }),
  24.   ],
  25. })
  26. ```

Add React Hook


The useSession()React Hook in the NextAuth.js client is the easiest way to check if someone is signed in.

  1. ``` js
  2. import { useSession, signIn, signOut } from "next-auth/react"

  3. export default function Component() {
  4.   const { data: session } = useSession()
  5.   if (session) {
  6.     return (
  7.       <>
  8.         Signed in as {session.user.email} <br />
  9.         <button onClick={() => signOut()}>Sign out</button>
  10.       </>
  11.     )
  12.   }
  13.   return (
  14.     <>
  15.       Not signed in <br />
  16.       <button onClick={() => signIn()}>Sign in</button>
  17.     </>
  18.   )
  19. }
  20. ```

Share/configure session state


Use the ``to allow instances of `useSession()`to share the session object across components. It also takes care of keeping the session updated and synced between tabs/windows.

  1. ``` js
  2. import { SessionProvider } from "next-auth/react"

  3. export default function App({
  4.   Component,
  5.   pageProps: { session, ...pageProps },
  6. }) {
  7.   return (
  8.     <SessionProvider session={session}>
  9.       <Component {...pageProps} />
  10.     </SessionProvider>
  11.   )
  12. }
  13. ```

Security


If you think you have found a vulnerability (or not sure) in NextAuth.js or any of the related packages (i.e. Adapters), we ask you to have a read of our Security Policy to reach out responsibly. Please do not open Pull Requests/Issues/Discussions before consulting with us.

Acknowledgments



undefined

Support


We're happy to announce we've recently created an OpenCollective for individuals and companies looking to contribute financially to the project!

|   |   |   |   |   |   |   |
| :--- | :--- | :--- | :--- | :--- | :--- | :--- |
| Vercel Logo Vercel 🥉 Bronze Financial Sponsor ☁️ Infrastructure Support | Prisma Logo Prisma 🥉 Bronze Financial Sponsor | Prisma Logo Clerk 🥉 Bronze Financial Sponsor | Lowdefy Logo Lowdefy 🥉 Bronze Financial Sponsor | WorkOS Logo WorkOS 🥉 Bronze Financial Sponsor | Checkly Logo Checkly         ☁️ Infrastructure Support | superblog Logo superblog         ☁️ Infrastructure Support  ||

Contributing


We're open to all community contributions! If you'd like to contribute in any way, please first read our Contributing Guide .

License


ISC