diff --git a/lib/Auth/sendEmail.ts b/lib/Auth/sendEmail.ts index 2f6f95e6..38ba9c60 100644 --- a/lib/Auth/sendEmail.ts +++ b/lib/Auth/sendEmail.ts @@ -14,13 +14,17 @@ const transporterCache: Map = new Map(); * https://www.nodemailer.com/transports/ses/ */ export const sendEmail = (smptConfig: SMTPConfig, email: Email) => { + const transporter = getOrSetTransporter(smptConfig); + return send(transporter, email); +} + +export const getOrSetTransporter = (smptConfig: SMTPConfig) => { const configStr = JSON.stringify(smptConfig); const transporter = transporterCache.get(configStr) ?? getTransporter(smptConfig); if(!transporterCache.has(configStr)){ transporterCache.set(configStr, transporter); } - - return send(transporter, email); + return transporter; } const getTransporter = (smptConfig: SMTPConfig) => { diff --git a/lib/Auth/setEmailProvider.ts b/lib/Auth/setEmailProvider.ts index 815777d5..9e688853 100644 --- a/lib/Auth/setEmailProvider.ts +++ b/lib/Auth/setEmailProvider.ts @@ -1,7 +1,7 @@ import e from "express"; import { AUTH_ROUTES_AND_PARAMS, AuthHandler, HTTPCODES } from "./AuthHandler"; import { Email, SMTPConfig } from "./AuthTypes"; -import { sendEmail } from "./sendEmail"; +import { getOrSetTransporter, sendEmail } from "./sendEmail"; import { promises } from "node:dns"; export async function setEmailProvider(this: AuthHandler, app: e.Express) { @@ -12,6 +12,14 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) { await checkDmarc(websiteUrl); } + if(email.signupType === "withPassword"){ + if(email.emailConfirmation){ + tryGetTransporter(email.emailConfirmation.smtp); + } + } else { + tryGetTransporter(email.emailMagicLink.smtp); + } + app.post(AUTH_ROUTES_AND_PARAMS.emailSignup, async (req, res) => { const { username, password } = req.body; let validationError = ""; @@ -82,4 +90,12 @@ const checkDmarc = async (websiteUrl: string) => { } else { console.log("DMARC set to reject") } +} + +const tryGetTransporter = (smtp: SMTPConfig) => { + try { + getOrSetTransporter(smtp); + } catch(err) { + console.error("Failed to set email transporter", err); + } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 76b57c1a..e72d6fc8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.2.161", + "version": "4.2.162", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.2.161", + "version": "4.2.162", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0", diff --git a/package.json b/package.json index 41c6a5e4..ee26a25b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prostgles-server", - "version": "4.2.161", + "version": "4.2.162", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/tests/server/package-lock.json b/tests/server/package-lock.json index 3de0d0b6..97716854 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.2.161", + "version": "4.2.162", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0",