pangolin/server/auth/sendEmailVerificationCode.ts
Milo Schwartz 235e91294e
remove base_url from config ()
* add example config dir, logos, and update CONTRIBUTING.md

* update dockerignore

* split base_url into dashboard_url and base_domain

* Remove unessicary ports

* Allow anything for the ip

* Update docker tags

* Complex regex for domains/ips

* update gitignore

---------

Co-authored-by: Owen Schwartz <owen@txv.io>
2025-01-07 22:41:35 -05:00

48 lines
1.5 KiB
TypeScript

import { TimeSpan, createDate } from "oslo";
import { generateRandomString, alphabet } from "oslo/crypto";
import db from "@server/db";
import { users, emailVerificationCodes } from "@server/db/schema";
import { eq } from "drizzle-orm";
import { sendEmail } from "@server/emails";
import config from "@server/lib/config";
import { VerifyEmail } from "@server/emails/templates/VerifyEmailCode";
export async function sendEmailVerificationCode(
email: string,
userId: string
): Promise<void> {
const code = await generateEmailVerificationCode(userId, email);
await sendEmail(
VerifyEmail({
username: email,
verificationCode: code,
verifyLink: `${config.getRawConfig().app.dashboard_url}/auth/verify-email`
}),
{
to: email,
from: config.getRawConfig().email?.no_reply,
subject: "Verify your email address"
}
);
}
async function generateEmailVerificationCode(
userId: string,
email: string
): Promise<string> {
const code = generateRandomString(8, alphabet("0-9"));
await db.transaction(async (trx) => {
await trx
.delete(emailVerificationCodes)
.where(eq(emailVerificationCodes.userId, userId));
await trx.insert(emailVerificationCodes).values({
userId,
email,
code,
expiresAt: createDate(new TimeSpan(15, "m")).getTime()
});
});
return code;
}