Files
cal.diy/packages/trpc/server/routers/viewer/auth/util.ts
T
Anik Dhabal Babu ee70111c45 fix: RateLimit verify email and fix (#23718)
* fix: RateLimit verify email

* update

* client chnages

* type error

* fix

* add test

* Update event-types.e2e.ts

* minor update

* Update event-types.e2e.ts

* fix test

* handle it in api v2
2025-09-12 18:27:05 +01:00

29 lines
806 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;
};