Skip to main content

Providers

Authentication Providers in NextAuth.js are services that can be used to sign in (OAuth, Email, etc).

Sign in with OAuth#

NextAuth.js is designed to work with any OAuth service, it supports OAuth 1.0, 1.0A and 2.0 and has built-in support for many popular OAuth sign-in services.

Built-in OAuth providers#

Using a built-in OAuth provider#

  1. Register your application at the developer portal of your provider. There are links above to the developer docs for most supported providers with details on how to register your application.

  2. The redirect URI should follow this format:

    [origin]/api/auth/callback/[provider]

    For example, Twitter on localhost this would be:

    http://localhost:3000/api/auth/callback/twitter
  3. Create a .env file at the root of your project and add the client ID and client secret. For Twitter this would be:

    TWITTER_ID=YOUR_TWITTER_CLIENT_ID
    TWITTER_SECRET=YOUR_TWITTER_CLIENT_SECRET
  4. Now you can add the provider settings to the NextAuth options object. You can add as many OAuth providers as you like, as you can see providers is an array.

    pages/api/auth/[...nextauth].js
    import Providers from `next-auth/providers`
    ...
    providers: [
    Providers.Twitter({
    clientId: process.env.TWITTER_ID,
    clientSecret: process.env.TWITTER_SECRET
    })
    ],
    ...
  5. Once a provider has been setup, you can sign in at the following URL: [origin]/api/auth/signin. This is an unbranded auto-generated page with all the configured providers.

Signin Screenshot

Using a custom provider#

You can use an OAuth provider that isn't built-in by using a custom object.

As an example of what this looks like, this is the provider object returned for the Google provider:

{
id: "google",
name: "Google",
type: "oauth",
version: "2.0",
scope: "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
params: { grant_type: "authorization_code" },
accessTokenUrl: "https://accounts.google.com/o/oauth2/token",
requestTokenUrl: "https://accounts.google.com/o/oauth2/auth",
authorizationUrl: "https://accounts.google.com/o/oauth2/auth?response_type=code",
profileUrl: "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
async profile(profile, tokens) {
// You can use the tokens, in case you want to fetch more profile information
// For example several OAuth provider does not return e-mail by default.
// Depending on your provider, will have tokens like `access_token`, `id_token` and or `refresh_token`
return {
id: profile.id,
name: profile.name,
email: profile.email,
image: profile.picture
}
},
clientId: "",
clientSecret: ""
}

You can replace all the options in this JSON object with the ones from your custom provider - be sure to give it a unique ID and specify the correct OAuth version - and add it to the providers option:

pages/api/auth/[...nextauth].js
import Providers from `next-auth/providers`
...
providers: [
Providers.Twitter({
clientId: process.env.TWITTER_ID,
clientSecret: process.env.TWITTER_SECRET,
}),
{
id: 'customProvider',
name: 'CustomProvider',
type: 'oauth',
version: '2.0',
scope: '' // Make sure to request the users email address
...
}
]
...

OAuth provider options#

NameDescriptionTypeRequired
idUnique ID for the providerstringYes
nameDescriptive name for the providerstringYes
typeType of provider, in this case it should be oauthoauth, email, credentialsYes
versionOAuth version (e.g. '1.0', '1.0a', '2.0')stringYes
accessTokenUrlEndpoint to retrieve an access tokenstringYes
authorizationUrlEndpoint to request authorization from the userstringYes
clientIdClient ID of the OAuth providerstringYes
clientSecretClient Secret of the OAuth providerstringNo
scopeOAuth access scopes (expects array or string)string or string[]No
paramsAdditional authorization URL parametersobjectNo
requestTokenUrlEndpoint to retrieve a request tokenstringNo
authorizationParamsAdditional params to be sent to the authorization endpointobjectNo
profileUrlEndpoint to retrieve the user's profilestringNo
profileAn callback returning an object with the user's infoobjectNo
idTokenSet to true for services that use ID Tokens (e.g. OpenID)booleanNo
headersAny headers that should be sent to the OAuth providerobjectNo
protectionAdditional security for OAuth login flows (defaults to state)[pkce],[state],[pkce,state]No
stateSame as protection: "state". Being deprecated, use protection.booleanNo

Sign in with Email#

The Email provider uses email to send "magic links" that can be used sign in, you will likely have seen them before if you have used software like Slack.

Adding support for signing in via email in addition to one or more OAuth services provides a way for users to sign in if they lose access to their OAuth account (e.g. if it is locked or deleted).

Configuration is similar to other providers, but the options are different:

pages/api/auth/[...nextauth].js
import Providers from `next-auth/providers`
...
providers: [
Providers.Email({
server: process.env.EMAIL_SERVER,
from: process.env.EMAIL_FROM,
// maxAge: 24 * 60 * 60, // How long email links are valid for (default 24h)
}),
],
...

See the Email provider documentation for more information on how to configure email sign in.

note

The email provider requires a database, it cannot be used without one.

Sign in with Credentials#

The Credentials provider allows you to handle signing in with arbitrary credentials, such as a username and password, two factor authentication or hardware device (e.g. YubiKey U2F / FIDO).

It is intended to support use cases where you have an existing system you need to authenticate users against.

pages/api/auth/[...nextauth].js
import Providers from `next-auth/providers`
...
providers: [
Providers.Credentials({
// The name to display on the sign in form (e.g. 'Sign in with...')
name: 'Credentials',
// The credentials is used to generate a suitable form on the sign in page.
// You can specify whatever fields you are expecting to be submitted.
// e.g. domain, username, password, 2FA token, etc.
credentials: {
username: { label: "Username", type: "text", placeholder: "jsmith" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
const user = (credentials) => {
// You need to provide your own logic here that takes the credentials
// submitted and returns either a object representing a user or value
// that is false/null if the credentials are invalid.
// e.g. return { id: 1, name: 'J Smith', email: 'jsmith@example.com' }
return null
}
if (user) {
// Any user object returned here will be saved in the JSON Web Token
return user
} else {
return null
}
}
})
]
...

See the Credentials provider documentation for more information.

note

The Credentials provider can only be used if JSON Web Tokens are enabled for sessions. Users authenticated with the Credentials provider are not persisted in the database.

Adding a new built-in provider#

If you think your custom provider might be useful to others, we encourage you to open a PR and add it to the built-in list so others can discover it much more easily! You only need to add two changes:

  1. Add your config: src/providers/{provider}.js (Make sure you use a named default export, like export default function YourProvider!)
  2. Add provider documentation: www/docs/providers/{provider}.md

That's it! ๐ŸŽ‰ Others will be able to discover this provider much more easily now!

You can look at the existing built-in providers for inspiration.