Files
calendar/packages/trpc/server/routers/viewer/auth/util.ts
T
2025-09-13 08:16:04 -03:00

29 lines
811 B
TypeScript

import { createHash } from "crypto";
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
import { hashEmail } from "@calcom/lib/server/PiiHasher";
import { totpRawCheck } from "@calcom/lib/totp";
export const verifyCodeUnAuthenticated = async (email: string, code: string) => {
if (!email || !code) {
throw new Error("Email and code are required");
}
await checkRateLimitAndThrowError({
rateLimitingType: "core",
identifier: `emailVerifyCode.${hashEmail(email)}`,
});
const secret = createHash("md5")
.update(email + (process.env.CALENDSO_ENCRYPTION_KEY || ""))
.digest("hex");
const isValidToken = totpRawCheck(code, secret, { step: 900 });
if (!isValidToken) {
throw new Error("Invalid verification code");
}
return true;
};