📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Added a confirmation route for changing email
Yevhenii Kulisidi78538efd9f0f
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.tsindex 28f8a7396..cf72235f0 100644--- a/src/auth/auth.controller.ts+++ b/src/auth/auth.controller.ts@@ -12,8 +12,8 @@ import {HttpStatus,Get,Param,- Headers,UnauthorizedException,+ Headers,} from '@nestjs/common'import {ApiTags,@@ -36,8 +36,9 @@ import {ConfirmResetPasswordDto,ResetPasswordDto,ChangePasswordDto,+ ConfirmChangeEmailDto,+ RequestChangeEmailDto,} from './dtos'-import { ChangeEmailDto } from './dtos/change-email.dto'import { JwtAccessTokenGuard } from './guards'@ApiTags('Auth')@@ -270,8 +271,8 @@ export class AuthController {description: 'User email changed',})@Post('change-email')- public async changeEmail(- @Body() body: ChangeEmailDto,+ public async requestChangeEmail(+ @Body() body: RequestChangeEmailDto,@CurrentUserId() userId: string,@I18n() i18n: I18nContext,): Promise<void> {@@ -298,4 +299,25 @@ export class AuthController {await this.authService.changeEmail(user.id, user.email, body.newEmail)}++ @ApiOperation({ summary: 'Confirm a user email change' })+ @ApiOkResponse({+ description: 'User email confirmed',+ })+ @Public()+ @Get('change-email/confirm/:token')+ public async confirmChangeEmail(+ @Param() params: ConfirmChangeEmailDto,+ @I18n() i18n: I18nContext,+ ): Promise<void> {+ const actionToken = await this.authService.checkChangeEmailToken(+ params.token,+ )++ if (!actionToken) {+ throw new ConflictException(i18n.t('auth.invalidChangeEmailToken'))+ }++ await this.authService.confirmChangeEmail(actionToken)+ }}diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.tsindex 311ca7330..c2804109d 100644--- a/src/auth/auth.service.ts+++ b/src/auth/auth.service.ts@@ -314,4 +314,27 @@ export class AuthService {},)}++ public async checkChangeEmailToken(+ token: string,+ ): Promise<ActionToken | null> {+ const actionToken = await this.actionTokensService.findActionToken(token)++ if (actionToken && actionToken.action === ActionTokenType.EMAIL_CHANGE) {+ return actionToken+ }++ return null+ }++ public async confirmChangeEmail(actionToken: ActionToken): Promise<void> {+ await this.userService.updateUser(actionToken.user.id, {+ email: actionToken.newValue,+ })+ await this.actionTokensService.deleteActionToken(actionToken.id)+ await this.mailerService.sendEmail(+ actionToken.newValue,+ LetterTemplate.MailAddressHadChanged,+ )+ }}diff --git a/src/auth/dtos/confirm-change-email.dto.ts b/src/auth/dtos/confirm-change-email.dto.tsnew file mode 100644index 000000000..978d62050--- /dev/null+++ b/src/auth/dtos/confirm-change-email.dto.ts@@ -0,0 +1,11 @@+import { ApiProperty } from '@nestjs/swagger'+import { IsUUID } from 'class-validator'++export class ConfirmChangeEmailDto {+ @ApiProperty({+ description: 'The token to confirm the change email',+ example: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6',+ })+ @IsUUID('4', { message: 'Please enter the valid UUID.' })+ public readonly token: string+}diff --git a/src/auth/dtos/index.ts b/src/auth/dtos/index.tsindex 7b916defc..453aa2b4f 100644--- a/src/auth/dtos/index.ts+++ b/src/auth/dtos/index.ts@@ -5,3 +5,5 @@ export * from './request-reset-password.dto'export * from './confirm-reset-password.dto'export * from './reset-password.dto'export * from './change-password.dto'+export * from './request-change-email.dto'+export * from './confirm-change-email.dto'diff --git a/src/auth/dtos/change-email.dto.ts b/src/auth/dtos/request-change-email.dto.tssimilarity index 94%rename from src/auth/dtos/change-email.dto.tsrename to src/auth/dtos/request-change-email.dto.tsindex b93a52567..c4e0d33ee 100644--- a/src/auth/dtos/change-email.dto.ts+++ b/src/auth/dtos/request-change-email.dto.ts@@ -2,7 +2,7 @@ import { ApiProperty } from '@nestjs/swagger'import { IsEmail } from 'class-validator'import { IsPassword } from '../decorators'-export class ChangeEmailDto {+export class RequestChangeEmailDto {@ApiProperty({description: 'User email',example: '[email protected]',diff --git a/src/i18n/en/auth.json b/src/i18n/en/auth.jsonindex a820acffa..556e909c5 100644--- a/src/i18n/en/auth.json+++ b/src/i18n/en/auth.json@@ -7,5 +7,6 @@"invalidPassword": "Invalid password.","emailAlreadyVerified": "Email already verified.","maxEmailRequestsReached": "Maximum number of email requests reached.",- "emailAlreadyTaken": "Email already taken."+ "emailAlreadyTaken": "Email already taken.",+ "invalidChangeEmailToken": "Invalid change email token."}