Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Added route to resend email verification

Yevhenii Kulisidi363c05c0e476
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index 5f06ce968..8510f2627 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -234,4 +234,33 @@ export class AuthController {
await this.authService.changePassword(user.id, body.newPassword)
}
+
+ @ApiOperation({ summary: 'Request a resend of the verification email' })
+ @ApiOkResponse({
+ description: 'Resend of the verification email requested',
+ })
+ @Post('verify-email')
+ public async requestResendVerificationEmail(
+ @CurrentUserId() userId: string,
+ @I18n() i18n: I18nContext,
+ ): Promise<void> {
+ const user = await this.userService.findUserById(userId)
+
+ if (!user) {
+ throw new UnauthorizedException()
+ }
+
+ const isMaxEmailRequestsReached =
+ await this.authService.checkIfMaxEmailRequestsReached(user.id)
+
+ if (isMaxEmailRequestsReached) {
+ throw new ConflictException(i18n.t('auth.maxEmailRequestsReached'))
+ }
+
+ if (user.isActive) {
+ throw new ConflictException(i18n.t('auth.emailAlreadyVerified'))
+ }
+
+ await this.authService.sendVerificationEmail(user.id, user.email)
+ }
}
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index 43c24d43a..c54749c28 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -14,7 +14,7 @@ import {
import { ActionTokensService } from 'src/action-tokens/action-tokens.service'
import { LetterTemplate } from 'src/mailer/letter'
import { MailerService } from 'src/mailer/mailer.service'
-import { User } from 'src/user/entities/user.entity'
+import { MAX_EMAIL_REQUESTS, User } from 'src/user/entities/user.entity'
import { TelegrafContext } from 'src/user/user.controller'
import { UserService } from 'src/user/user.service'
import { Telegraf } from 'telegraf'
@@ -69,12 +69,20 @@ export class AuthService {
return await hash(password, salt)
}
- private async sendVerificationEmail(userId: string, email: string) {
+ public async sendVerificationEmail(userId: string, email: string) {
const actionToken = await this.actionTokensService.createActionToken(
userId,
ActionTokenType.EMAIL_VERIFICATION,
)
+ const user = await this.userService.findUserById(userId)
+
+ if (user && user.emailRequests < MAX_EMAIL_REQUESTS) {
+ await this.userService.updateUser(userId, {
+ emailRequests: user.emailRequests + 1,
+ })
+ }
+
const verificationLink = `${this.configService.get(
'CLIENT_URL',
)}/verify-email/${actionToken.id}`
@@ -265,4 +273,16 @@ export class AuthService {
password: hashedPassword,
})
}
+
+ public async checkIfMaxEmailRequestsReached(
+ userId: string,
+ ): Promise<boolean> {
+ const user = await this.userService.findUserById(userId)
+
+ if (user && user.emailRequests >= MAX_EMAIL_REQUESTS) {
+ return true
+ }
+
+ return false
+ }
}
diff --git a/src/i18n/en/auth.json b/src/i18n/en/auth.json
index 0ee7d74d8..71797700e 100644
--- a/src/i18n/en/auth.json
+++ b/src/i18n/en/auth.json
@@ -4,5 +4,7 @@
"invalidVerificationToken": "Invalid verification token.",
"accountNotExists": "An account with this email does not exist.",
"invalidResetPasswordToken": "Invalid reset password token.",
- "invalidPassword": "Invalid password."
+ "invalidPassword": "Invalid password.",
+ "emailAlreadyVerified": "Email already verified.",
+ "maxEmailRequestsReached": "Maximum number of email requests reached."
}