The Email provider uses email to send "magic links" that can be used to sign in, you will likely have seen these if you have used services like Slack before.
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).
The Email provider can be used in conjunction with (or instead of) one or more OAuth providers.
On initial sign in, a Verification Token is sent to the email address provided. By default this token is valid for 24 hours. If the Verification Token is used with that time (i.e. by clicking on the link in the email) an account is created for the user and they are signed in.
If someone provides the email address of an existing account when signing in, an email is sent and they are signed into the account associated with that email address when they follow the link in the email.
The Email Provider can be used with both JSON Web Tokens and database sessions, but you must configure a database to use it. It is not possible to enable email sign in without using a database.
The Email Provider comes with a set of default options:
You can override any of the options to suit your own use case.
- You will need an SMTP account; ideally for one of the services known to work with nodemailer.
- There are two ways to configure the SMTP server connection.
You can either use a connection string or a nodemailer configuration object.
2.1 Using a connection string
Create an .env file to the root of your project and add the connection string and email address.
Now you can add the email provider like this:
2.2 Using a configuration object
.env file in the root of your project simply add the configuration object options individually:
Now you can add the provider settings to the NextAuth options object in the Email Provider.
- You can now sign in with an email address at
A user account (i.e. an entry in the Users table) will not be created for the user until the first time they verify their email address. If an email address is already associated with an account, the user will be signed in to that account when they use the link in the email.
You can fully customise the sign in email that is sent by passing a custom function as the
sendVerificationRequest option to
The following code shows the complete source for the built-in
If you want to generate great looking email client compatible HTML with React, check out https://mjml.io
By default, we are generating a random verification token. You can define a
generateVerificationToken method in your provider options if you want to override it: