SendGrid

Use SendGrid to send magic link emails

Generate and API Key on SendGrid and copy it down.

Set it into the .env file

SENDGRID_API_KEY="<your_api_key>"

Open src/config/auth.ts and add the provider

providers: [
  EmailProvider({
    async sendVerificationRequest({identifier: email, url}) {
      // Call the cloud Email provider API for sending emails
      // See https://docs.sendgrid.com/api-reference/mail-send/mail-send
      const response = await fetch("https://api.sendgrid.com/v3/mail/send", {
        // The body format will vary depending on provider, please see their documentation
        // for further details.
        body: JSON.stringify({
          personalizations: [{ to: [{ email }] }],
          from: { email: "noreply@company.com" },
          subject: "Sign in to Your page",
          content: [
            {
              type: "text/plain",
              value: `Please click here to authenticate - ${url}`,
            },
          ],
        }),
        headers: {
          // Authentication will also vary from provider to provider, please see their docs.
          Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`,
          "Content-Type": "application/json",
        },
        method: "POST",
      })

      if (!response.ok) {
        const { errors } = await response.json()
        throw new Error(JSON.stringify(errors))
      }
    },
  })
],

Last updated