Skip to content

Commit

Permalink
Add email authentication via postmark
Browse files Browse the repository at this point in the history
  • Loading branch information
venables committed Apr 22, 2023
1 parent 34fe7ed commit 14d984e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ NEXTAUTH_SECRET="changeme"
NEXTAUTH_URL="$HOST"
GOOGLE_CLIENT_ID="XXXX"
GOOGLE_CLIENT_SECRET="XXXX"

# Email (Authentication)
SMTP_FROM=StartKit <no-reply@startkit.dev>
POSTMARK_API_TOKEN="changeme"
POSTMARK_SIGN_IN_TEMPLATE_ALIAS="sign-in"
POSTMARK_ACTIVATION_TEMPLATE_ALIAS="welcome"
57 changes: 57 additions & 0 deletions lib/auth/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import EmailProvider from "next-auth/providers/email"
import GoogleProvider from "next-auth/providers/google"
import { Client } from "postmark"

import { siteConfig } from "@/config"
import { prisma } from "@/lib/database"

import { fullURL } from "../url-fns"

import type { NextAuthOptions } from "next-auth"

const postmarkClient = new Client(process.env.POSTMARK_API_TOKEN!)

export const authOptions: NextAuthOptions = {
/**
* https://authjs.dev/reference/adapter/prisma
Expand All @@ -15,6 +22,56 @@ export const authOptions: NextAuthOptions = {
* https://next-auth.js.org/providers/
*/
providers: [
/**
*
*/
EmailProvider({
from: process.env.SMTP_FROM,
sendVerificationRequest: async ({ identifier, url, provider }) => {
const user = await prisma.user.findUnique({
where: {
email: identifier
},
select: {
emailVerified: true
}
})

const templateAlias = user?.emailVerified
? process.env.POSTMARK_SIGN_IN_TEMPLATE_ALIAS
: process.env.POSTMARK_ACTIVATION_TEMPLATE_ALIAS

if (!templateAlias) {
throw new Error("Missing template id")
}

const result = await postmarkClient.sendEmailWithTemplate({
To: identifier,
From: provider.from as string,
TemplateAlias: templateAlias,
TemplateModel: {
product_url: fullURL(),
product_name: siteConfig.name,
name: identifier,
action_url: url,
company_name: siteConfig.company.name,
company_address: siteConfig.company.link
},
Headers: [
{
// Set this to prevent Gmail from threading emails.
// See https://stackoverflow.com/questions/23434110/force-emails-not-to-be-grouped-into-conversations/25435722.
Name: "X-Entity-Ref-ID",
Value: new Date().getTime() + ""
}
]
})

if (result.ErrorCode) {
throw new Error(result.Message)
}
}
}),
/**
* https://next-auth.js.org/providers/google
*/
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"next-auth": "^4.22.1",
"next-themes": "^0.2.1",
"nextjs-google-analytics": "^2.3.3",
"nodemailer": "^6.9.1",
"postmark": "^3.0.15",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.43.9",
Expand Down
44 changes: 41 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 14d984e

@vercel
Copy link

@vercel vercel bot commented on 14d984e Apr 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.