You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// when started working on https://github.com/nextauthjs/next-auth/discussions/1465
82
114
exporttypeEmailProviderType="email"
83
115
84
-
/**
116
+
/**
85
117
* ## Overview
86
118
* 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.
87
-
*
119
+
*
88
120
* 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).
89
-
*
121
+
*
90
122
* The Email provider can be used in conjunction with (or instead of) one or more OAuth providers.
91
123
* ### How it works
92
-
*
124
+
*
93
125
* 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 within that time (i.e. by clicking on the link in the email) an account is created for the user and they are signed in.
94
-
*
95
-
*
126
+
*
127
+
*
96
128
* 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.
97
-
*
129
+
*
98
130
* :::tip
99
131
* 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.
100
132
* :::
@@ -103,20 +135,20 @@ export type EmailProviderType = "email"
103
135
* 1. NextAuth.js does not include `nodemailer` as a dependency, so you'll need to install it yourself if you want to use the Email Provider. Run `npm install nodemailer` or `yarn add nodemailer`.
104
136
* 2. You will need an SMTP account; ideally for one of the [services known to work with `nodemailer`](https://community.nodemailer.com/2-0-0-beta/setup-smtp/well-known-services/).
105
137
* 3. There are two ways to configure the SMTP server connection.
106
-
*
138
+
*
107
139
* You can either use a connection string or a `nodemailer` configuration object.
108
-
*
140
+
*
109
141
* 3.1 **Using a connection string**
110
-
*
142
+
*
111
143
* Create an `.env` file to the root of your project and add the connection string and email address.
* Now you can add the provider settings to the NextAuth.js options object in the Email Provider.
144
-
*
176
+
*
145
177
* ```js title="pages/api/auth/[...nextauth].js"
146
178
* import EmailProvider from "next-auth/providers/email";
147
179
* ...
@@ -159,19 +191,19 @@ export type EmailProviderType = "email"
159
191
* }),
160
192
* ],
161
193
* ```
162
-
*
194
+
*
163
195
* 4. Do not forget to setup one of the database [adapters](https://authjs.dev/reference/adapters) for storing the Email verification token.
164
-
*
196
+
*
165
197
* 5. You can now sign in with an email address at `/api/auth/signin`.
166
-
*
198
+
*
167
199
* 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.
168
-
*
200
+
*
169
201
* ## Customizing emails
170
-
*
202
+
*
171
203
* You can fully customize the sign in email that is sent by passing a custom function as the `sendVerificationRequest` option to `EmailProvider()`.
* If you want to generate great looking email client compatible HTML with React, check out https://mjml.io
272
304
* :::
273
-
*
305
+
*
274
306
* ## Customizing the Verification Token
275
-
*
307
+
*
276
308
* 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:
277
-
*
309
+
*
278
310
* ```js title="pages/api/auth/[...nextauth].js"
279
311
* providers: [
280
312
* EmailProvider({
@@ -284,9 +316,9 @@ export type EmailProviderType = "email"
284
316
* })
285
317
* ],
286
318
* ```
287
-
*
319
+
*
288
320
* ## Normalizing the email address
289
-
*
321
+
*
290
322
* By default, Auth.js will normalize the email address. It treats values as case-insensitive (which is technically not compliant to the [RFC 2821 spec](https://datatracker.ietf.org/doc/html/rfc2821), but in practice this causes more problems than it solves, eg. when looking up users by e-mail from databases.) and also removes any secondary email address that was passed in as a comma-separated list. You can apply your own normalization via the `normalizeIdentifier` method on the `EmailProvider`. The following example shows the default behavior:
291
323
* ```ts
292
324
* EmailProvider({
@@ -299,7 +331,7 @@ export type EmailProviderType = "email"
299
331
* // but we remove it on the domain part
300
332
* domain = domain.split(",")[0]
301
333
* return `${local}@${domain}`
302
-
*
334
+
*
303
335
* // You can also throw an error, which will redirect the user
304
336
* // to the sign-in page with error=EmailSignin in the URL
305
337
* // if (identifier.split("@").length > 2) {
@@ -308,12 +340,12 @@ export type EmailProviderType = "email"
308
340
* },
309
341
* })
310
342
* ```
311
-
*
343
+
*
312
344
* :::warning
313
345
* Always make sure this returns a single e-mail address, even if multiple ones were passed in.
0 commit comments