diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index 2daf7dd..0834c54 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -6,7 +6,7 @@ import GoogleProvider from "next-auth/providers/google"; import EmailProvider from 'next-auth/providers/email' import prisma from '../../../lib/prisma' import { Session } from "@prisma/client"; - +import { CustomsendVerificationRequest } from "./signinemail" const authHandler: NextApiHandler = (req, res) => NextAuth(req, res, options); export default authHandler; @@ -27,7 +27,10 @@ const options = { pass: process.env.EMAIL_SERVER_PASSWORD } }, - from: process.env.EMAIL_FROM + from: process.env.EMAIL_FROM, + sendVerificationRequest({ identifier, url, provider }) { + CustomsendVerificationRequest({ identifier, url, provider }) + } }), ], adapter: PrismaAdapter(prisma), diff --git a/pages/api/auth/signinemail.ts b/pages/api/auth/signinemail.ts new file mode 100644 index 0000000..9f63511 --- /dev/null +++ b/pages/api/auth/signinemail.ts @@ -0,0 +1,243 @@ +// https://craftingemails.com/blog/nextauth-signin-custom-email +import { Theme } from "next-auth" +import { createTransport } from "nodemailer" + +export async function CustomsendVerificationRequest(params) { + const { identifier, url, provider, theme } = params + const { host } = new URL(url) + // NOTE: You are not required to use `nodemailer`, use whatever you want. + const transport = createTransport(provider.server) + const result = await transport.sendMail({ + to: identifier, + from: provider.from, + subject: `Sign in to ${host}`, + text: text({ url, host }), + html: html({ url, host, theme }), + }) + const failed = result.rejected.concat(result.pending).filter(Boolean) + if (failed.length) { + throw new Error(`Email(s) (${failed.join(", ")}) could not be sent`) + } +} + +function html(params: { url: string; host: string; theme: Theme }) { + const { url, host, theme } = params + + const escapedHost = host.replace(/\./g, "​.") + + return ` + + + + + + + + + + + Connection à Thé Tip Top + + + + + + +
+ Connexion à Thé Tip Top + ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ +
+
+ + + + +
+ +
+
+
+ + + + + + +
+
+ +` +} + +// Email Text body (fallback for email clients that don't render HTML) +function text({ url, host }: { url: string; host: string }) { + return `Connectez vous à ${host}\n${url}\n\n` +} \ No newline at end of file