mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-14 07:55:02 +02:00
verify email workflow working
This commit is contained in:
parent
e176295593
commit
76eeb335a3
23 changed files with 16363 additions and 15802 deletions
41
server/routers/auth/sendEmailVerificationCode.ts
Normal file
41
server/routers/auth/sendEmailVerificationCode.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
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 VerifyEmail from "@server/emails/templates/verifyEmailCode";
|
||||
import env from "@server/environment";
|
||||
|
||||
export async function sendEmailVerificationCode(
|
||||
email: string,
|
||||
userId: string,
|
||||
): Promise<void> {
|
||||
const code = await generateEmailVerificationCode(userId, email);
|
||||
|
||||
await sendEmail(VerifyEmail({ username: email, verificationCode: code }), {
|
||||
to: email,
|
||||
from: env.EMAIL_NOREPLY!,
|
||||
subject: "Verify your email address",
|
||||
});
|
||||
}
|
||||
|
||||
async function generateEmailVerificationCode(
|
||||
userId: string,
|
||||
email: string,
|
||||
): Promise<string> {
|
||||
await db
|
||||
.delete(emailVerificationCodes)
|
||||
.where(eq(emailVerificationCodes.userId, userId));
|
||||
|
||||
const code = generateRandomString(8, alphabet("0-9"));
|
||||
|
||||
await db.insert(emailVerificationCodes).values({
|
||||
userId,
|
||||
email,
|
||||
code,
|
||||
expiresAt: createDate(new TimeSpan(15, "m")).getTime(),
|
||||
});
|
||||
|
||||
return code;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue