feat: show dialog when changing email after using Google Login (#9611)
Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Alex van Andel <[email protected]> Co-authored-by: alannnc <[email protected]> Co-authored-by: zomars <[email protected]>
This commit is contained in:
10 files changed
+169
-103
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { TFunction } from "next-i18next";
|
||||
import type { TFunction } from "next-i18next";
|
||||
|
||||
import { APP_NAME } from "@calcom/lib/constants";
|
||||
|
||||
@@ -14,8 +14,6 @@ export type PasswordReset = {
|
||||
resetLink: string;
|
||||
};
|
||||
|
||||
export const PASSWORD_RESET_EXPIRY_HOURS = 6;
|
||||
|
||||
export default class ForgotPasswordEmail extends BaseEmail {
|
||||
passwordEvent: PasswordReset;
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { User } from "@prisma/client";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { sendPasswordResetEmail } from "@calcom/emails";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
export const PASSWORD_RESET_EXPIRY_HOURS = 6;
|
||||
|
||||
const RECENT_MAX_ATTEMPTS = 3;
|
||||
const RECENT_PERIOD_IN_MINUTES = 5;
|
||||
|
||||
const createPasswordReset = async (email: string): Promise<string> => {
|
||||
const expiry = dayjs().add(PASSWORD_RESET_EXPIRY_HOURS, "hours").toDate();
|
||||
const createdResetPasswordRequest = await prisma.resetPasswordRequest.create({
|
||||
data: {
|
||||
email,
|
||||
expires: expiry,
|
||||
},
|
||||
});
|
||||
|
||||
return `${process.env.NEXT_PUBLIC_WEBAPP_URL}/auth/forgot-password/${createdResetPasswordRequest.id}`;
|
||||
};
|
||||
|
||||
const guardAgainstTooManyPasswordResets = async (email: string) => {
|
||||
const recentPasswordRequestsCount = await prisma.resetPasswordRequest.count({
|
||||
where: {
|
||||
email,
|
||||
createdAt: {
|
||||
gt: dayjs().subtract(RECENT_PERIOD_IN_MINUTES, "minutes").toDate(),
|
||||
},
|
||||
},
|
||||
});
|
||||
if (recentPasswordRequestsCount >= RECENT_MAX_ATTEMPTS) {
|
||||
throw new Error("Too many password reset attempts. Please try again later.");
|
||||
}
|
||||
};
|
||||
const passwordResetRequest = async (user: Pick<User, "email" | "name" | "locale">) => {
|
||||
const { email } = user;
|
||||
const t = await getTranslation(user.locale ?? "en", "common");
|
||||
await guardAgainstTooManyPasswordResets(email);
|
||||
const resetLink = await createPasswordReset(email);
|
||||
// send email in user language
|
||||
await sendPasswordResetEmail({
|
||||
language: t,
|
||||
user,
|
||||
resetLink,
|
||||
});
|
||||
};
|
||||
|
||||
export { passwordResetRequest };
|
||||
@@ -19,7 +19,7 @@ function detectTransport(): SendmailTransport.Options | SMTPConnection.Options |
|
||||
},
|
||||
secure: port === 465,
|
||||
tls: {
|
||||
rejectUnauthorized: isENVDev ? false : true,
|
||||
rejectUnauthorized: !isENVDev,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import type { NextApiResponse, GetServerSidePropsContext } from "next";
|
||||
import type { GetServerSidePropsContext, NextApiResponse } from "next";
|
||||
|
||||
import stripe from "@calcom/app-store/stripepayment/lib/server";
|
||||
import { getPremiumPlanProductId } from "@calcom/app-store/stripepayment/lib/utils";
|
||||
import { passwordResetRequest } from "@calcom/features/auth/lib/passwordResetRequest";
|
||||
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
||||
import { getTranslation } from "@calcom/lib/server";
|
||||
import { checkUsername } from "@calcom/lib/server/checkUsername";
|
||||
@@ -11,6 +12,7 @@ import slugify from "@calcom/lib/slugify";
|
||||
import { updateWebUser as syncServicesUpdateWebUser } from "@calcom/lib/sync/SyncServiceManager";
|
||||
import { validateBookerLayouts } from "@calcom/lib/validateBookerLayouts";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { IdentityProvider } from "@calcom/prisma/enums";
|
||||
import { userMetadata } from "@calcom/prisma/zod-utils";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
@@ -33,6 +35,9 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
|
||||
metadata: input.metadata as Prisma.InputJsonValue,
|
||||
};
|
||||
|
||||
// some actions can invalidate a user session.
|
||||
let signOutUser = false;
|
||||
let passwordReset = false;
|
||||
let isPremiumUsername = false;
|
||||
|
||||
const layoutError = validateBookerLayouts(input?.metadata?.defaultBookerLayouts || null);
|
||||
@@ -56,16 +61,7 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
|
||||
if (input.avatar) {
|
||||
data.avatar = await resizeBase64Image(input.avatar);
|
||||
}
|
||||
const userToUpdate = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!userToUpdate) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "User not found" });
|
||||
}
|
||||
const metadata = userMetadata.parse(userToUpdate.metadata);
|
||||
const metadata = userMetadata.parse(user.metadata);
|
||||
|
||||
const isPremium = metadata?.isPremium;
|
||||
if (isPremiumUsername) {
|
||||
@@ -98,12 +94,26 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
|
||||
});
|
||||
}
|
||||
}
|
||||
const hasEmailBeenChanged = userToUpdate.email !== data.email;
|
||||
const hasEmailBeenChanged = data.email && user.email !== data.email;
|
||||
|
||||
if (hasEmailBeenChanged) {
|
||||
data.emailVerified = null;
|
||||
}
|
||||
|
||||
// check if we are changing email and identity provider is not CAL
|
||||
const hasEmailChangedOnNonCalProvider =
|
||||
hasEmailBeenChanged && user.identityProvider !== IdentityProvider.CAL;
|
||||
const hasEmailChangedOnCalProvider = hasEmailBeenChanged && user.identityProvider === IdentityProvider.CAL;
|
||||
|
||||
if (hasEmailChangedOnNonCalProvider) {
|
||||
// Only validate if we're changing email
|
||||
data.identityProvider = IdentityProvider.CAL;
|
||||
data.identityProviderId = null;
|
||||
} else if (hasEmailChangedOnCalProvider) {
|
||||
// when the email changes, the user needs to sign in again.
|
||||
signOutUser = true;
|
||||
}
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
@@ -113,12 +123,23 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
|
||||
id: true,
|
||||
username: true,
|
||||
email: true,
|
||||
identityProvider: true,
|
||||
identityProviderId: true,
|
||||
metadata: true,
|
||||
name: true,
|
||||
createdDate: true,
|
||||
locale: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (hasEmailChangedOnNonCalProvider) {
|
||||
// Because the email has changed, we are now attempting to use the CAL provider-
|
||||
// which has no password yet. We have to send the reset password email.
|
||||
await passwordResetRequest(updatedUser);
|
||||
signOutUser = true;
|
||||
passwordReset = true;
|
||||
}
|
||||
|
||||
// Sync Services
|
||||
await syncServicesUpdateWebUser(updatedUser);
|
||||
|
||||
@@ -154,5 +175,5 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
|
||||
.then(() => console.info("Booking pages revalidated"))
|
||||
.catch((e) => console.error(e));
|
||||
}*/
|
||||
return input;
|
||||
return { ...input, signOutUser, passwordReset };
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { sendPasswordResetEmail } from "@calcom/emails";
|
||||
import { PASSWORD_RESET_EXPIRY_HOURS } from "@calcom/emails/templates/forgot-password-email";
|
||||
import { PASSWORD_RESET_EXPIRY_HOURS } from "@calcom/features/auth/lib/passwordResetRequest";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
|
||||
Reference in new issue
Block a user