Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions docs/docs/guides/providers/email-http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ title: HTTP-based Email Provider
The following guide is written for `next-auth` (NextAuth.js), but it should work for any of the Auth.js framework libraries (`@auth/*`) as well.
:::


There is a built-in Email provider with which you could connect to the SMTP server of your choice to send "magic link" emails for sign-in purposes. However, the Email provider can also be used with HTTP-based email services, like AWS SES, Postmark, Sendgrid, etc. In this guide, we are going to explain how to use our Email magic link provider with any of the more modern HTTP-based Email APIs.

For this example, we will be using [SendGrid](https://sendgrid.com), but any email service providing an HTTP API or JS client library will work.
Expand All @@ -27,18 +26,18 @@ First, if you do not have a project using Auth.js, clone and set up a basic Auth
import NextAuth, { NextAuthOptions } from "next-auth"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"
import Email from "next-auth/providers/email"

const prisma = new PrismaClient()

export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
providers: [
{
id: 'sendgrid',
type: 'email',
async sendVerificationRequest({identifier: email, url}) {
}
}
Email({
id: "sendgrid",
type: "email",
async sendVerificationRequest({ identifier: email, url }) {},
}),
],
}

Expand All @@ -51,6 +50,7 @@ As mentioned earlier, we're going to be using SendGrid in this example, so the a

```js title="pages/api/auth/[...nextauth].ts"
import NextAuth, { NextAuthOptions } from "next-auth"
import Email from "next-auth/providers/email"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { PrismaClient } from "@prisma/client"

Expand All @@ -59,10 +59,10 @@ const prisma = new PrismaClient()
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
providers: [
{
id: 'sendgrid',
type: 'email',
async sendVerificationRequest({identifier: email, url}) {
Email({
id: "sendgrid",
type: "email",
async sendVerificationRequest({ identifier: email, url }) {
// highlight-start
// Call the cloud Email provider API for sending emails
// See https://docs.sendgrid.com/api-reference/mail-send/mail-send
Expand Down Expand Up @@ -94,7 +94,7 @@ export const authOptions: NextAuthOptions = {
}
// highlight-end
},
}
}),
],
}
```
Expand Down
21 changes: 18 additions & 3 deletions packages/core/src/providers/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ import * as SMTPPool from "nodemailer/lib/smtp-pool/index.js"
import * as StreamTransport from "nodemailer/lib/stream-transport/index.js"

// TODO: Make use of https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html for the string
type AllTransportOptions = string | SMTPTransport | SMTPTransport.Options | SMTPPool | SMTPPool.Options | SendmailTransport | SendmailTransport.Options | StreamTransport | StreamTransport.Options | JSONTransport | JSONTransport.Options | SESTransport | SESTransport.Options | Transport<any> | TransportOptions
type AllTransportOptions =
| string
| SMTPTransport
| SMTPTransport.Options
| SMTPPool
| SMTPPool.Options
| SendmailTransport
| SendmailTransport.Options
| StreamTransport
| StreamTransport.Options
| JSONTransport
| JSONTransport.Options
| SESTransport
| SESTransport.Options
| Transport<any>
| TransportOptions

export interface SendVerificationRequestParams {
identifier: string
Expand All @@ -36,6 +51,7 @@ export interface SendVerificationRequestParams {
* [Custom email service with Auth.js](https://authjs.dev/guides/providers/email#custom-email-service)
*/
export interface EmailUserConfig {
id?: "email"
server?: AllTransportOptions
type?: "email"
/** @default `"Auth.js <[email protected]>"` */
Expand Down Expand Up @@ -90,7 +106,7 @@ export interface EmailConfig extends CommonProviderOptions {
id: "email"
type: "email"
name: "Email"
server: AllTransportOptions
server?: AllTransportOptions
from: string
maxAge: number
sendVerificationRequest: (
Expand All @@ -109,7 +125,6 @@ export interface EmailConfig extends CommonProviderOptions {
normalizeIdentifier?: (identifier: string) => string
}


// TODO: Rename to Token provider
// when started working on https://github.com/nextauthjs/next-auth/discussions/1465
export type EmailProviderType = "email"
Expand Down