📈 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 route to change email
Yevhenii Kulisidi0c649e1caed2
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.tsindex 8510f2627..28f8a7396 100644--- a/src/auth/auth.controller.ts+++ b/src/auth/auth.controller.ts@@ -37,6 +37,7 @@ import {ResetPasswordDto,ChangePasswordDto,} from './dtos'+import { ChangeEmailDto } from './dtos/change-email.dto'import { JwtAccessTokenGuard } from './guards'@ApiTags('Auth')@@ -263,4 +264,38 @@ export class AuthController {await this.authService.sendVerificationEmail(user.id, user.email)}++ @ApiOperation({ summary: 'Change a user email' })+ @ApiOkResponse({+ description: 'User email changed',+ })+ @Post('change-email')+ public async changeEmail(+ @Body() body: ChangeEmailDto,+ @CurrentUserId() userId: string,+ @I18n() i18n: I18nContext,+ ): Promise<void> {+ const user = await this.userService.findUserById(userId)++ if (!user) {+ throw new UnauthorizedException()+ }++ const isPasswordValid = await this.authService.validateUser(+ user.email,+ body.password,+ )++ if (!isPasswordValid) {+ throw new ConflictException(i18n.t('auth.invalidPassword'))+ }++ const isEmailTaken = await this.authService.checkIfEmailTaken(body.newEmail)++ if (isEmailTaken) {+ throw new ConflictException(i18n.t('auth.emailAlreadyTaken'))+ }++ await this.authService.changeEmail(user.id, user.email, body.newEmail)+ }}diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.tsindex c54749c28..311ca7330 100644--- a/src/auth/auth.service.ts+++ b/src/auth/auth.service.ts@@ -285,4 +285,33 @@ export class AuthService {return false}++ public async checkIfEmailTaken(email: string): Promise<boolean> {+ const user = await this.userService.findUser(email)+ return Boolean(user)+ }++ public async changeEmail(+ userId: string,+ email: string,+ newEmail: string,+ ): Promise<void> {+ const actionToken = await this.actionTokensService.createActionToken(+ userId,+ ActionTokenType.EMAIL_CHANGE,+ newEmail,+ )++ const verificationLink = `${this.configService.get(+ 'CLIENT_URL',+ )}/change-email/${actionToken.id}`++ await this.mailerService.sendEmail(+ email,+ LetterTemplate.MailAddressChangeConfirmation,+ {+ url: verificationLink,+ },+ )+ }}diff --git a/src/auth/dtos/change-email.dto.ts b/src/auth/dtos/change-email.dto.tsnew file mode 100644index 000000000..b93a52567--- /dev/null+++ b/src/auth/dtos/change-email.dto.ts@@ -0,0 +1,23 @@+import { ApiProperty } from '@nestjs/swagger'+import { IsEmail } from 'class-validator'+import { IsPassword } from '../decorators'++export class ChangeEmailDto {+ @ApiProperty({+ description: 'User email',+ example: '[email protected]',+ maxLength: 254,+ minLength: 6,+ })+ @IsEmail({}, { message: 'Please enter the valid email.' })+ public readonly newEmail: string++ @ApiProperty({+ description: 'User password',+ example: '%d7*c4W45p',+ maxLength: 72,+ minLength: 8,+ })+ @IsPassword({ message: 'Please enter the valid password.' })+ public readonly password: string+}diff --git a/src/i18n/en/auth.json b/src/i18n/en/auth.jsonindex 71797700e..a820acffa 100644--- a/src/i18n/en/auth.json+++ b/src/i18n/en/auth.json@@ -6,5 +6,6 @@"invalidResetPasswordToken": "Invalid reset password token.","invalidPassword": "Invalid password.","emailAlreadyVerified": "Email already verified.",- "maxEmailRequestsReached": "Maximum number of email requests reached."+ "maxEmailRequestsReached": "Maximum number of email requests reached.",+ "emailAlreadyTaken": "Email already taken."}