Skip to content

Commit

Permalink
feat: email smtp
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Aug 8, 2024
1 parent c8ddad2 commit 258438c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
6 changes: 1 addition & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=
UNDB_BASE_URL=http://localhost:3721
24 changes: 11 additions & 13 deletions apps/backend/src/modules/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion apps/backend/src/modules/mail/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
}

Expand Down
10 changes: 10 additions & 0 deletions packages/env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 258438c

Please sign in to comment.