Overview
The Email provider uses email to send "magic links" that can be used 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 conjuction with (or instead of) one or more OAuth providers.
How it works
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 signin 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.
tip
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.
Configuration
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.
.envEMAIL_SERVER=smtp://username:password@smtp.example.com:587EMAIL_FROM=noreply@example.comNow you can add the email provider like this:
/pages/api/auth/[...nextauth].jsproviders: [Providers.Email({server: process.env.EMAIL_SERVER,from: process.env.EMAIL_FROM}),],2.2 Using a configuration object
In your
.envfile in the root of your project simply add the configuration object options individually:.envEMAIL_SERVER_USER=usernameEMAIL_SERVER_PASSWORD=passwordEMAIL_SERVER_HOST=smtp.example.comEMAIL_SERVER_PORT=587EMAIL_FROM=noreply@example.comNow you can add the provider settings to the NextAuth options object in the Email Provider.
/pages/api/auth/[...nextauth].jsproviders: [Providers.Email({server: {host: process.env.EMAIL_SERVER_HOST,port: process.env.EMAIL_SERVER_PORT,auth: {user: process.env.EMAIL_SERVER_USER,pass: process.env.EMAIL_SERVER_PASSWORD}},from: process.env.EMAIL_FROM}),],You can now sign in with an email address at
/api/auth/signin.An account will not be created for the user until the first time they verify their email address. If an email address already assoicated with an account, the user will be signed in to that account when they use the link in the email.
Customising emails
You can fully customise the sign in email that is sent by passing a custom function as the sendVerificationRequest option to Providers.Email().
The following example shows the complete source for the built-in sendVerificationRequest() method.
tip
If you want to generate email-client compatible HTML from React, check out https://mjml.io