Download tasks/linkedin/solution/site/implementation/clone/lib/email-verification.ts from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 3.34 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/lib/email-verification.ts
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/solution/site/implementation/clone/lib/email-verification.ts
-
curl -L -o email-verification.ts https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/lib/email-verification.ts
3.34 kB
| import { createHmac, randomBytes } from "node:crypto"; | |
| import { and, eq, gt, isNull } from "drizzle-orm"; | |
| import { getDb } from "@/db"; | |
| import { emailVerificationTokens, users, type User } from "@/db/schema"; | |
| import { ApiError } from "./api"; | |
| import { appUrl } from "./app-url"; | |
| import { sendTransactionalEmail } from "./email"; | |
| const tokenHours = 24; | |
| function verificationSecret() { | |
| const configured = process.env.EMAIL_VERIFICATION_SECRET ?? process.env.SESSION_SECRET; | |
| if (configured && configured.length >= 32) return configured; | |
| if (process.env.NODE_ENV === "production") throw new Error("EMAIL_VERIFICATION_SECRET or SESSION_SECRET must contain at least 32 characters in production."); | |
| return "local-development-email-verification-secret"; | |
| } | |
| export function hashVerificationToken(token: string) { | |
| return createHmac("sha256", verificationSecret()).update(token).digest("hex"); | |
| } | |
| function shouldExposePreviewUrl() { | |
| return process.env.NODE_ENV !== "production" && process.env.APP_ENV !== "production"; | |
| } | |
| export async function sendVerificationEmail(user: Pick<User, "id" | "email" | "name">) { | |
| const db = getDb(); | |
| const token = randomBytes(32).toString("base64url"); | |
| const issuedAt = new Date(); | |
| const expiresAt = new Date(issuedAt.getTime() + tokenHours * 60 * 60_000); | |
| const verificationUrl = `${appUrl()}/verify-email?token=${encodeURIComponent(token)}`; | |
| await db.update(emailVerificationTokens) | |
| .set({ consumedAt: issuedAt }) | |
| .where(and(eq(emailVerificationTokens.userId, user.id), isNull(emailVerificationTokens.consumedAt))); | |
| await db.insert(emailVerificationTokens).values({ | |
| userId: user.id, | |
| tokenHash: hashVerificationToken(token), | |
| sentToEmail: user.email, | |
| expiresAt, | |
| }); | |
| const delivery = await sendTransactionalEmail({ | |
| to: user.email, | |
| subject: "Verify your LinkedIn account", | |
| previewUrl: verificationUrl, | |
| text: `Hi ${user.name},\n\nVerify your LinkedIn account with this link: ${verificationUrl}\n\nThis link expires in ${tokenHours} hours and only verifies your independent LinkedIn account.`, | |
| html: `<p>Hi ${user.name},</p><p>Verify your independent LinkedIn account:</p><p><a href="${verificationUrl}">Verify email</a></p><p>This link expires in ${tokenHours} hours.</p>`, | |
| }); | |
| return { verificationPreviewUrl: shouldExposePreviewUrl() ? delivery.previewUrl : undefined }; | |
| } | |
| export async function verifyEmailToken(token: string) { | |
| const db = getDb(); | |
| const [row] = await db.select({ token: emailVerificationTokens, user: users }) | |
| .from(emailVerificationTokens) | |
| .innerJoin(users, eq(users.id, emailVerificationTokens.userId)) | |
| .where(and( | |
| eq(emailVerificationTokens.tokenHash, hashVerificationToken(token)), | |
| isNull(emailVerificationTokens.consumedAt), | |
| gt(emailVerificationTokens.expiresAt, new Date()), | |
| )) | |
| .limit(1); | |
| if (!row) throw new ApiError(400, "This verification link is invalid or expired.", "invalid_verification_token"); | |
| const verifiedAt = row.user.emailVerifiedAt ?? new Date(); | |
| await db.update(users).set({ emailVerifiedAt: verifiedAt, updatedAt: new Date() }).where(eq(users.id, row.user.id)); | |
| await db.update(emailVerificationTokens).set({ consumedAt: new Date() }).where(eq(emailVerificationTokens.id, row.token.id)); | |
| return { ...row.user, emailVerifiedAt: verifiedAt }; | |
| } | |