Files
calendar/packages/trpc/server/routers/viewer/workflows/verifyEmailCode.handler.ts
T
bd6c471652 feat: Workflow — Send Email to Specific Email (#14815)
* feat: Workflow — Send Email to Specific Email

* disable button when verifyEmail pending

* add verified emails table and routes

* fix: include teamId in verifyEmailCode

* fix: include email address in activate handler

* fix: check team before adding email

* feat: check verified email, add to team if only users

* remove logs

* fix: if statement

Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com>

* change email to verify your email

* fix: show error if saving unverified email

* fix: show email in subject

* verify email when editing steps

* remove double calls from same block

* verify email when creating new step

---------

Co-authored-by: v0ltZzie <161201747+v0ltZzie@users.noreply.github.com>
Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com>
2024-05-20 21:23:19 +00:00

42 lines
1.1 KiB
TypeScript

import { createHash } from "crypto";
import { totpRawCheck } from "@calcom/lib/totp";
import { prisma } from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import { TRPCError } from "@trpc/server";
import type { TVerifyEmailCodeInputSchema } from "./verifyEmailCode.schema";
type VerifyEmailCodeOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TVerifyEmailCodeInputSchema;
};
export const verifyEmailCodeHandler = async ({ ctx, input }: VerifyEmailCodeOptions) => {
const { code, email, teamId } = input;
const { id } = ctx.user;
if (!code || !email) throw new TRPCError({ code: "BAD_REQUEST" });
const secret = createHash("md5")
.update(email + process.env.CALENDSO_ENCRYPTION_KEY)
.digest("hex");
const isValidToken = totpRawCheck(code, secret, { step: 900 });
if (!isValidToken) throw new TRPCError({ code: "BAD_REQUEST", message: "invalid_code" });
await prisma.verifiedEmail.create({
data: {
email,
userId: id,
teamId,
},
});
return isValidToken;
};