Options
Environment Variables
NEXTAUTH_URL
When deploying to production, set the NEXTAUTH_URL
environment variable to the canonical URL of your site.
If your Next.js application uses a custom base path, specify the route to the API endpoint in full.
e.g. NEXTAUTH_URL=https://example.com/custom-route/api/auth
tip
To set environment variables on Vercel, you can use the dashboard or the now env
command.
Options
Options are passed to NextAuth.js when initializing it in an API route.
providers
- Default value:
[]
- Required: Yes
Description
An array of authentication providers for signing in (e.g. Google, Facebook, Twitter, GitHub, Email, etc) in any order. This can be one of the built-in providers or an object with a custom provider.
See the providers documentation for a list of supported providers and how to use them.
database
- Default value:
null
- Required: No (Except by Email provider)
Description
A database connection string or TypeORM configuration object.
NextAuth.js has built in support for MySQL, MariaDB, Postgress, MongoDB and SQLite databases.
The default database provider is also compatible with other ANSI SQL compatible databases.
NextAuth.js can can be used with any database by specifying a custom adapter
option.
tip
The Email provider currently requires a database to be configured.
secret
- Default value:
string
(SHA hash of the "options" object) - Required: No (but strongly recommended)
Description
A random string used to hash tokens and sign cookies (e.g. SHA hash).
If not provided will be auto-generated based on hash of all your provided options.
The default behaviour is secure, but volatile, and it is strongly recommended you explicitly specify a value for secret to avoid invalidating any tokens when the automatically generated hash changes.
session
- Default value:
object
- Required: No
Description
The session
object and all properties on it are optional.
Default values for this option are shown below:
jwt
- Default value:
object
- Required: No
Description
JSON Web Tokens are can be used for session tokens (instead of database sessions) if enabled with session: { jwt: true }
option is set. They are enabled by default if you have not specified a database.
You can use JSON Web Tokens for session data in conjuction with a database for user data, or you can use JSON Web Tokens without a database.
Options and values for JSON Web Tokens:
An example JSON WebToken contains an encrypted payload like this:
JWT Helper
You can use the built-in getToken()
helper method to verify and decrypt the token, like this:
For convenience, this helper function is also able to read and decode tokens passed in an HTTP Bearer header.
Required
The getToken() helper requires the following options:
- req - (object) Request object (required)
- secret - (string) JWT Secret (required)
You must also pass any options configured on the jwt
option to the helper.
e.g. Including custom session maxAge
and custom signing and/or encryption keys or options
Optional
It also supports the following options:
secureCookie - (boolean) Uses secure prefixed cookie if true (optional)
By default, the helper function will attempt to determine if it should use the secure prefixed cookie (e.g.
true
in production,false
in development, unless NEXTAUTH_URL is configured with an HTTPS URL).cookieName - (string) Session token cookie name (optional)
The
secureCookie
option is ignored ifcookieName
is explcitly specified.
note
The JWT is stored in the Session Token cookie, the same cookie used for tokens with database sessions.
pages
- Default value:
{}
- Required: No
Description
Specify URLs to be used if you want to create custom sign in, sign out and error pages.
Pages specified will override the corresponding built-in page.
For example:
See the documentation for the pages option for more information.
callbacks
- Default value:
object
- Required: No
Description
Callbacks are asynchronous functions you can use to control what happens when an action is performed.
Callbacks are extremely powerful, especially in scenarios involving JSON Web Tokens as they allow you to implement access controls without a database and to integrate with external databases or APIs.
You can specify a handler for any of the callbacks below.
See the callbacks documentation for more information on how to use the callback functions.
events
- Default value:
object
- Required: No
Description
Events are asynchronous functions that do not return a response, they are useful for audit logging.
You can specify a handler for any of these events below - e.g. for debugging or to create an audit log.
The content of the message object varies depending on the flow (e.g. OAuth or Email authentication flow, JWT or database sessions, etc), but typically contains a user object and/or contents of the JSON Web Token and other information relevent to the event.
adapter
- Default value: Adapater.Default()
- Required: No
Description
By default NextAuth.js uses a database adapter that uses TypeORM and supports MySQL, MariaDB, Postgres and MongoDB and SQLite databases. An alternative adapter that uses Prisma, which currently supports MySQL, MariaDB and Postgres, is also included.
You can use the adapter
option to use the Prisma adapter - or pass in your own adapter if you want to use a database that is not supported by one of the built-in adapters.
See the adapter documentation for more information.
note
If the adapter
option is specified it overrides the database
option, only specify one or the other.
debug
- Default value:
false
- Required: No
Description
Set debug to true
to enable debug messages for authentication and database operations.
Advanced Options
Advanced options are passed the same way as basic options, but may have complex implications or side effects. You should try to avoid using advanced options unless you are very comfortable using them.
useSecureCookies
- Default value:
true
for HTTPS sites /false
for HTTP sites - Required: No
Description
When set to true
(the default for all site URLs that start with https://
) then all cookies set by NextAuth.js will only be accessible from HTTPS URLs.
This option defaults to false
on URLs that start with http://
(e.g. http://localhost:3000
) for developer convenience.
You can manually set this option to false
to disable this security feature and allow cookies to be accessible from non-secured URLs (this is not recommended).
note
Properties on any custom cookies
that are specified override this option.
warning
Setting this option to false in production is a security risk and may allow sessions to hijacked if used in production. It is intended to support development and testing. Using this option is not recommended.
cookies
- Default value:
{}
- Required: No
Description
You can override the default cookie names and options for any of the cookies used by NextAuth.js.
This is an advanced option and using it is not recommended as you may break authentication or introduce security flaws into your application.
You can specify one or more cookies with custom properties, but if you specify custom options for a cookie you must provided all the options for it. You will also likely want to create conditional behaviour to support local development (e.g. setting secure: false
and not using cookie prefixes on localhost URLs).
For example:
warning
Using a custom cookie policy may introduce security flaws into your application and is intended as an option for advanced users who understand the implications. Using this option is not recommended.