Skip to content

The open alternative to Salesforce, designed for AI.

Apply the verification email cooldown to email updates (#29093)

Charles Bochet10803a9fdeb3
diff --git a/packages/twenty-server/src/engine/core-modules/email-verification/services/email-verification.service.ts b/packages/twenty-server/src/engine/core-modules/email-verification/services/email-verification.service.ts
index 81ed73fbcf2..b389bf24b44 100644
--- a/packages/twenty-server/src/engine/core-modules/email-verification/services/email-verification.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/email-verification/services/email-verification.service.ts
@@ -147,38 +147,52 @@ export class EmailVerificationService {
);
}
+ await this.deleteExistingTokensAfterCooldownOrThrow({ userId: user.id });
+
+ await this.sendVerificationEmail({
+ userId: user.id,
+ email,
+ workspace,
+ locale,
+ verificationTrigger: EmailVerificationTrigger.SIGN_UP,
+ });
+
+ return { success: true };
+ }
+
+ async deleteExistingTokensAfterCooldownOrThrow({
+ userId,
+ }: {
+ userId: string;
+ }) {
const existingToken = await this.appTokenRepository.findOne({
where: {
- userId: user.id,
+ userId,
type: AppTokenType.EmailVerificationToken,
},
+ order: { createdAt: 'DESC' },
});
- if (existingToken) {
- const cooldownDuration = ms('1m');
- const timeToWaitMs = differenceInMilliseconds(
- addMilliseconds(existingToken.createdAt, cooldownDuration),
- new Date(),
- );
+ if (!isDefined(existingToken)) {
+ return;
+ }
- if (timeToWaitMs > 0) {
- throw new EmailVerificationException(
- `Please wait ${ms(timeToWaitMs, { long: true })} before requesting another verification email`,
- EmailVerificationExceptionCode.RATE_LIMIT_EXCEEDED,
- );
- }
+ const cooldownDuration = ms('1m');
+ const timeToWaitMs = differenceInMilliseconds(
+ addMilliseconds(existingToken.createdAt, cooldownDuration),
+ new Date(),
+ );
- await this.appTokenRepository.delete(existingToken.id);
+ if (timeToWaitMs > 0) {
+ throw new EmailVerificationException(
+ `Please wait ${ms(timeToWaitMs, { long: true })} before requesting another verification email`,
+ EmailVerificationExceptionCode.RATE_LIMIT_EXCEEDED,
+ );
}
- await this.sendVerificationEmail({
- userId: user.id,
- email,
- workspace,
- locale,
- verificationTrigger: EmailVerificationTrigger.SIGN_UP,
+ await this.appTokenRepository.delete({
+ userId,
+ type: AppTokenType.EmailVerificationToken,
});
-
- return { success: true };
}
}
diff --git a/packages/twenty-server/src/engine/core-modules/user/services/user.service.ts b/packages/twenty-server/src/engine/core-modules/user/services/user.service.ts
index efbb31d7716..eac6575d3c4 100644
--- a/packages/twenty-server/src/engine/core-modules/user/services/user.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/user/services/user.service.ts
@@ -592,6 +592,10 @@ export class UserService {
workspace,
);
+ await this.emailVerificationService.deleteExistingTokensAfterCooldownOrThrow(
+ { userId: user.id },
+ );
+
await this.emailVerificationService.sendVerificationEmail({
userId: user.id,
email: normalizedEmail,
diff --git a/packages/twenty-server/src/engine/core-modules/user/user.resolver.ts b/packages/twenty-server/src/engine/core-modules/user/user.resolver.ts
index f6a6305c364..35e6f0a5939 100644
--- a/packages/twenty-server/src/engine/core-modules/user/user.resolver.ts
+++ b/packages/twenty-server/src/engine/core-modules/user/user.resolver.ts
@@ -55,6 +55,7 @@ import { AuthProvider } from 'src/engine/decorators/auth/auth-provider.decorator
import { AuthImpersonationContext } from 'src/engine/decorators/auth/auth-impersonation-context.decorator';
import { canCredentialAutoLoginIntoWorkspaces } from 'src/engine/core-modules/auth/utils/can-credential-auto-login-into-workspaces.util';
import { AuthGraphqlApiExceptionFilter } from 'src/engine/core-modules/auth/filters/auth-graphql-api-exception.filter';
+import { EmailVerificationExceptionFilter } from 'src/engine/core-modules/email-verification/email-verification-exception-filter.util';
import { AuthUserWorkspaceId } from 'src/engine/decorators/auth/auth-user-workspace-id.decorator';
import { AuthIsUserSession } from 'src/engine/decorators/auth/auth-is-user-session.decorator';
import { AuthUser } from 'src/engine/decorators/auth/auth-user.decorator';
@@ -770,6 +771,7 @@ export class UserResolver {
}),
SettingsPermissionGuard(PermissionFlagType.PROFILE_INFORMATION),
)
+ @UseFilters(EmailVerificationExceptionFilter)
async updateUserEmail(
@Args() { newEmail, verifyEmailRedirectPath }: UpdateUserEmailInput,
@AuthUser() user: AuthContextUser,