Skip to content

Commit f48eb04

Browse files
reconbotbalazsorban44
andauthoredJul 11, 2023
fix(providers): fix nodemailer/required types (#7950)
Co-authored-by: Balázs Orbán <info@balazsorban.com>
1 parent b25a090 commit f48eb04

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed
 

‎docs/docs/configuration/providers/email.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ The email provider requires a database, it cannot be used without one.
4141

4242
| Name | Description | Type | Required |
4343
| :---------------------: | :---------------------------------------------------------------------------------: | :------------------------------: | :------: |
44-
| id | Unique ID for the provider | `string` | Yes |
45-
| name | Descriptive name for the provider | `string` | Yes |
46-
| type | Type of provider, in this case `email` | `"email"` | Yes |
47-
| server | Path or object pointing to the email server | `string` or `Object` | Yes |
48-
| sendVerificationRequest | Callback to execute when a verification request is sent | `(params) => Promise<undefined>` | Yes |
44+
| id | Unique ID for the provider | `string` | No |
45+
| name | Descriptive name for the provider | `string` | No |
46+
| type | Type of provider, in this case `email` | `"email"` | No |
47+
| server | Path or object pointing to the email server | `string` or `Object` | No |
48+
| sendVerificationRequest | Callback to execute to send a verification request, default uses nodemailer | `(params) => Promise<undefined>` | No |
4949
| from | The email address from which emails are sent, default: "<no-reply@example.com>" | `string` | No |
5050
| maxAge | How long until the e-mail can be used to log the user in seconds. Defaults to 1 day | `number` | No |

‎docs/docs/providers/email.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ You can override any of the options to suit your own use case.
3636
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/).
3737
3. There are two ways to configure the SMTP server connection.
3838

39-
You can either use a connection string or a `nodemailer` configuration object.
39+
You can either use a connection string or a `nodemailer` configuration object or transport.
4040

4141
2.1 **Using a connection string**
4242

‎packages/next-auth/src/providers/email.ts

+38-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { createTransport } from "nodemailer"
2-
3-
import type { CommonProviderOptions } from "."
4-
import type { Options as SMTPTransportOptions } from "nodemailer/lib/smtp-transport"
1+
import { Transport, TransportOptions, createTransport } from "nodemailer"
2+
import * as JSONTransport from "nodemailer/lib/json-transport.js"
3+
import * as SendmailTransport from "nodemailer/lib/sendmail-transport/index.js"
4+
import * as SESTransport from "nodemailer/lib/ses-transport.js"
5+
import * as SMTPPool from "nodemailer/lib/smtp-pool/index.js"
6+
import * as SMTPTransport from "nodemailer/lib/smtp-transport.js"
7+
import * as StreamTransport from "nodemailer/lib/stream-transport.js"
58
import type { Awaitable } from ".."
9+
import type { CommonProviderOptions } from "."
610
import type { Theme } from "../core/types"
711

12+
// TODO: Make use of https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html for the string
13+
type AllTransportOptions = string | SMTPTransport | SMTPTransport.Options | SMTPPool | SMTPPool.Options | SendmailTransport | SendmailTransport.Options | StreamTransport | StreamTransport.Options | JSONTransport | JSONTransport.Options | SESTransport | SESTransport.Options | Transport<any> | TransportOptions
14+
815
export interface SendVerificationRequestParams {
916
identifier: string
1017
url: string
@@ -14,10 +21,9 @@ export interface SendVerificationRequestParams {
1421
theme: Theme
1522
}
1623

17-
export interface EmailConfig extends CommonProviderOptions {
18-
type: "email"
19-
// TODO: Make use of https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html
20-
server: string | SMTPTransportOptions
24+
export interface EmailUserConfig {
25+
server?: AllTransportOptions
26+
type?: "email"
2127
/** @default "NextAuth <no-reply@example.com>" */
2228
from?: string
2329
/**
@@ -27,7 +33,7 @@ export interface EmailConfig extends CommonProviderOptions {
2733
*/
2834
maxAge?: number
2935
/** [Documentation](https://next-auth.js.org/providers/email#customizing-emails) */
30-
sendVerificationRequest: (
36+
sendVerificationRequest?: (
3137
params: SendVerificationRequestParams
3238
) => Awaitable<void>
3339
/**
@@ -61,10 +67,31 @@ export interface EmailConfig extends CommonProviderOptions {
6167
* [Documentation](https://next-auth.js.org/providers/email#normalizing-the-e-mail-address) | [RFC 2821](https://tools.ietf.org/html/rfc2821) | [Email syntax](https://en.wikipedia.org/wiki/Email_address#Syntax)
6268
*/
6369
normalizeIdentifier?: (identifier: string) => string
64-
options: EmailUserConfig
6570
}
6671

67-
export type EmailUserConfig = Partial<Omit<EmailConfig, "options">>
72+
export interface EmailConfig extends CommonProviderOptions {
73+
// defaults
74+
id: "email"
75+
type: "email"
76+
name: "Email"
77+
server: AllTransportOptions
78+
from: string
79+
maxAge: number
80+
sendVerificationRequest: (
81+
params: SendVerificationRequestParams
82+
) => Awaitable<void>
83+
84+
/**
85+
* This is copied into EmailConfig in parseProviders() don't use elsewhere
86+
*/
87+
options: EmailUserConfig
88+
89+
// user options
90+
// TODO figure out a better way than copying from EmailUserConfig
91+
secret?: string
92+
generateVerificationToken?: () => Awaitable<string>
93+
normalizeIdentifier?: (identifier: string) => string
94+
}
6895

6996
export type EmailProvider = (options: EmailUserConfig) => EmailConfig
7097

‎packages/next-auth/src/providers/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface CommonProviderOptions {
1818
id: string
1919
name: string
2020
type: ProviderType
21-
options?: Record<string, unknown>
21+
options?: any
2222
}
2323

2424
export type Provider = OAuthConfig<any> | EmailConfig | CredentialsConfig

1 commit comments

Comments
 (1)
Please sign in to comment.