From 258438c2cb0569b8f59a6b3dde6f54cbef95073c Mon Sep 17 00:00:00 2001 From: nichenqin Date: Thu, 8 Aug 2024 14:59:15 +0800 Subject: [PATCH] feat: email smtp --- .env.example | 6 +----- apps/backend/src/modules/auth/auth.ts | 24 +++++++++++------------- apps/backend/src/modules/mail/mail.ts | 8 +++++++- packages/env/src/index.ts | 10 ++++++++++ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/.env.example b/.env.example index 0d8ebdff3..da12df90e 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1 @@ -UNDB_DB_TURSO_URL=libsql://127.0.0.1:8080?tls=0 -UNDB_DB_TURSO_AUTH_TOKEN= - -GITHUB_CLIENT_ID= -GITHUB_CLIENT_SECRET= \ No newline at end of file +UNDB_BASE_URL=http://localhost:3721 diff --git a/apps/backend/src/modules/auth/auth.ts b/apps/backend/src/modules/auth/auth.ts index 3da8863f0..73cd024e1 100644 --- a/apps/backend/src/modules/auth/auth.ts +++ b/apps/backend/src/modules/auth/auth.ts @@ -58,7 +58,7 @@ export class Auth { user_id: userId, email, code, - expires_at: createDate(new TimeSpan(15, "m")), + expires_at: createDate(new TimeSpan(15, "m")).getTime(), }) .execute() return code @@ -350,19 +350,17 @@ export class Auth { }) } - await withTransaction(this.queryBuilder)(async () => { - const validCode = await this.#verifyVerificationCode(user, code) - if (!validCode) { - throw new Error("Invalid code") - } + const validCode = await this.#verifyVerificationCode(user, code) + if (!validCode) { + throw new Error("Invalid code") + } - await this.lucia.invalidateUserSessions(user.id) - await this.queryBuilder - .updateTable("undb_user") - .set("email_verified", true) - .where("id", "=", user.id) - .execute() - }) + await this.lucia.invalidateUserSessions(user.id) + await (getCurrentTransaction() ?? this.queryBuilder) + .updateTable("undb_user") + .set("email_verified", true) + .where("id", "=", user.id) + .execute() const session = await this.lucia.createSession(user.id, { space_id: validatedSession.spaceId }) const sessionCookie = this.lucia.createSessionCookie(session.id) diff --git a/apps/backend/src/modules/mail/mail.ts b/apps/backend/src/modules/mail/mail.ts index 02663be44..d709c6d0d 100644 --- a/apps/backend/src/modules/mail/mail.ts +++ b/apps/backend/src/modules/mail/mail.ts @@ -8,7 +8,13 @@ import { compile } from "./templates/compile" function createMailerTransport() { return createTransport({ host: env.UNDB_MAIL_HOST, - port: parseInt(env.UNDB_MAIL_PORT ?? "", 10), + port: env.UNDB_MAIL_PORT ? parseInt(env.UNDB_MAIL_PORT, 10) : undefined, + secure: env.UNDB_MAIL_SECURE, + auth: { + user: env.UNDB_MAIL_USER, + pass: env.UNDB_MAIL_PASS, + }, + debug: Bun.env.NODE_ENV !== "production", }) } diff --git a/packages/env/src/index.ts b/packages/env/src/index.ts index 46d0ae89d..43c7d89d2 100644 --- a/packages/env/src/index.ts +++ b/packages/env/src/index.ts @@ -50,6 +50,8 @@ const s3Env = createEnv({ UNDB_S3_SECRET_ACCESS_KEY: z.string().optional(), UNDB_S3_STORAGE_ENDPOINT: z.string().url().optional(), UNDB_S3_STORAGE_REGION: z.string().optional(), + UNDB_MAIL_USER: z.string().optional(), + UNDB_MAIL_PASS: z.string().optional(), }, runtimeEnv: import.meta.env, emptyStringAsUndefined: true, @@ -69,6 +71,14 @@ const emailEnv = createEnv({ UNDB_MAIL_HOST: z.string().optional(), UNDB_MAIL_PORT: z.string().optional(), UNDB_MAIL_DEFAULT_FROM: z.string().optional(), + UNDB_MAIL_SECURE: z + .string() + .optional() + .default("false") + .refine((v) => v === "true" || v === "false", { + message: "UNDB_MAIL_SECURE must be a boolean", + }) + .transform((v) => v === "true"), UNDB_VERIFY_EMAIL: z .string() .optional()