* Implement secondary email * Fix already existing tests failing * Added tests for secondary email feature * Skip email verification if user is changing the primary email to a verified secondary email * Fix type errors in tests * Fix email becoming unverified when switched between primary and secondary email * Added a check to prevent prisma error from throwing up due to duplicate records * Improved error handling when adding a secondary email * Add test for resend verification email flow for secondary emails and validation of invite link * Add a new column to link secondary emails with verification tokens * Fix failing to update email to an unverified secondary email of the same user * Fix failing tests * Change text of resend verification email * Add ability to use the verified secondary emails to get the event details to * Fix type errors * Fix failing e2e tests * Fix failing unit tests * Fix failing type checks * Fix secondary verification email subject * Fix failing e2e tests --------- Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
|
import logger from "@calcom/lib/logger";
|
|
import { prisma } from "@calcom/prisma";
|
|
import { TRPCError } from "@calcom/trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../trpc";
|
|
import type { TResendVerifyEmailSchema } from "./resendVerifyEmail.schema";
|
|
|
|
type ResendEmailOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TResendVerifyEmailSchema;
|
|
};
|
|
|
|
const log = logger.getSubLogger({ prefix: [`[[Auth] `] });
|
|
|
|
export const resendVerifyEmail = async ({ input, ctx }: ResendEmailOptions) => {
|
|
let emailToVerify = ctx.user.email;
|
|
let emailVerified = Boolean(ctx.user.emailVerified);
|
|
let secondaryEmail;
|
|
// If the input which is coming is not the current user's email, it could be a secondary email
|
|
if (input?.email && input?.email !== ctx.user.email) {
|
|
secondaryEmail = await prisma.secondaryEmail.findUnique({
|
|
where: {
|
|
email: input.email,
|
|
userId: ctx.user.id,
|
|
},
|
|
});
|
|
|
|
if (!secondaryEmail) {
|
|
throw new TRPCError({ code: "BAD_REQUEST", message: "Email not found" });
|
|
}
|
|
|
|
if (secondaryEmail.emailVerified) {
|
|
emailVerified = true;
|
|
} else {
|
|
emailToVerify = input.email;
|
|
emailVerified = false;
|
|
}
|
|
}
|
|
if (emailVerified) {
|
|
log.info(`User ${ctx.user.id} already verified email`);
|
|
return {
|
|
ok: true,
|
|
skipped: true,
|
|
};
|
|
}
|
|
|
|
const email = await sendEmailVerification({
|
|
email: emailToVerify,
|
|
username: ctx.user?.username ?? undefined,
|
|
language: ctx.user.locale,
|
|
secondaryEmailId: secondaryEmail?.id,
|
|
});
|
|
|
|
return email;
|
|
};
|