33 lines
863 B
TypeScript
33 lines
863 B
TypeScript
import { createHash } from "crypto";
|
|
import { totp } from "otplib";
|
|
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { ZVerifyCodeInputSchema } from "./verifyCode.schema";
|
|
|
|
type VerifyCodeOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: ZVerifyCodeInputSchema;
|
|
};
|
|
|
|
export const verifyCodeHandler = async ({ ctx, input }: VerifyCodeOptions) => {
|
|
const { email, code } = input;
|
|
const { user } = ctx;
|
|
|
|
if (!user || !email || !code) throw new TRPCError({ code: "BAD_REQUEST" });
|
|
|
|
const secret = createHash("md5")
|
|
.update(email + process.env.CALENDSO_ENCRYPTION_KEY)
|
|
.digest("hex");
|
|
|
|
const isValidToken = totp.check(code, secret);
|
|
|
|
if (!isValidToken) throw new TRPCError({ code: "BAD_REQUEST", message: "invalid_code" });
|
|
|
|
return isValidToken;
|
|
};
|