📈 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 route for email verification
Yevhenii Kulisidie9d39ce37d66
diff --git a/src/action-tokens/action-tokens.service.ts b/src/action-tokens/action-tokens.service.tsindex 615e87833..03715a67e 100644--- a/src/action-tokens/action-tokens.service.ts+++ b/src/action-tokens/action-tokens.service.ts@@ -49,4 +49,14 @@ export class ActionTokensService {newValue,})}++ public async findActionToken(token: string) {+ return await this.actionTokensRepository.findOne(token, {+ relations: ['user'],+ })+ }++ public async deleteActionToken(token: string) {+ await this.actionTokensRepository.delete(token)+ }}diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.tsindex a17e8a6d8..54322ff31 100644--- a/src/auth/auth.controller.ts+++ b/src/auth/auth.controller.ts@@ -11,8 +11,15 @@ import {ConflictException,Headers,HttpCode,+ Param,+ Get,} from '@nestjs/common'-import { ApiTags, ApiOperation, ApiCreatedResponse } from '@nestjs/swagger'+import {+ ApiTags,+ ApiOperation,+ ApiCreatedResponse,+ ApiOkResponse,+} from '@nestjs/swagger'import { I18nValidationExceptionFilter, I18n, I18nContext } from 'nestjs-i18n'import { checkRateLimit } from 'src/common/utils'import { UserService } from 'src/user/user.service'@@ -22,6 +29,7 @@ import {RegisterRequestDto,LoginResponseDto,LoginRequestDto,+ VerifyEmailDto,} from './dtos'@ApiTags('Auth')@@ -85,7 +93,7 @@ export class AuthController {}@ApiOperation({ summary: 'Login a user' })- @ApiCreatedResponse({+ @ApiOkResponse({description: 'User logged in',type: LoginResponseDto,})@@ -117,4 +125,24 @@ export class AuthController {return { accessToken }}++ @ApiOperation({ summary: 'Verify a user email' })+ @ApiOkResponse({+ description: 'User email verified',+ })+ @Get('verify-email/:token')+ public async verifyEmail(+ @Param() params: VerifyEmailDto,+ @I18n() i18n: I18nContext,+ ): Promise<void> {+ const actionToken = await this.authService.checkVerificationToken(+ params.token,+ )++ if (!actionToken) {+ throw new ConflictException(i18n.t('auth.invalidVerificationToken'))+ }++ await this.authService.verifyEmail(actionToken)+ }}diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.tsindex 57b2471eb..71a0f3c66 100644--- a/src/auth/auth.module.ts+++ b/src/auth/auth.module.ts@@ -15,6 +15,7 @@ import { JwtAccessTokenStrategy } from './strategies'UserModule,ActionTokensModule,MailerModule,+ ActionTokensModule,],controllers: [AuthController],providers: [AuthService, JwtAccessTokenStrategy],diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.tsindex 99d53cb1a..3b2ada6ce 100644--- a/src/auth/auth.service.ts+++ b/src/auth/auth.service.ts@@ -7,7 +7,10 @@ import { getCountry } from 'countries-and-timezones'import { createHash } from 'crypto'import * as dayjs from 'dayjs'import { InjectBot } from 'nestjs-telegraf'-import { ActionTokenType } from 'src/action-tokens/action-token.entity'+import {+ ActionToken,+ ActionTokenType,+} from 'src/action-tokens/action-token.entity'import { ActionTokensService } from 'src/action-tokens/action-tokens.service'import { LetterTemplate } from 'src/mailer/letter'import { MailerService } from 'src/mailer/mailer.service'@@ -190,4 +193,26 @@ export class AuthService {const chat = (await this.telegramBot.telegram.getChat(chatId)).idreturn Boolean(chat)}++ public async checkVerificationToken(+ token: string,+ ): Promise<ActionToken | null> {+ const actionToken = await this.actionTokensService.findActionToken(token)++ if (+ actionToken &&+ actionToken.action === ActionTokenType.EMAIL_VERIFICATION+ ) {+ return actionToken+ }++ return null+ }++ public async verifyEmail(actionToken: ActionToken): Promise<void> {+ await this.userService.updateUser(actionToken.user.id, {+ isActive: true,+ })+ await this.actionTokensService.deleteActionToken(actionToken.id)+ }}diff --git a/src/auth/dtos/index.ts b/src/auth/dtos/index.tsindex 923dd2b34..db1d60442 100644--- a/src/auth/dtos/index.ts+++ b/src/auth/dtos/index.ts@@ -1,2 +1,3 @@export * from './register.dto'export * from './login.dto'+export * from './verify-email.dto'diff --git a/src/auth/dtos/verify-email.dto.ts b/src/auth/dtos/verify-email.dto.tsnew file mode 100644index 000000000..4a0c2e55e--- /dev/null+++ b/src/auth/dtos/verify-email.dto.ts@@ -0,0 +1,11 @@+import { ApiProperty } from '@nestjs/swagger'+import { IsUUID } from 'class-validator'++export class VerifyEmailDto {+ @ApiProperty({+ description: 'The token to verify the email',+ example: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6',+ })+ @IsUUID('4', { message: 'validation.isUuid' })+ token: string+}diff --git a/src/i18n/en/auth.json b/src/i18n/en/auth.jsonindex 504fb34ad..48822e5d8 100644--- a/src/i18n/en/auth.json+++ b/src/i18n/en/auth.json@@ -1,4 +1,5 @@{"leakedPassword": "The password is in the leak database.",- "invalidCredentials": "Incorrect email or password."+ "invalidCredentials": "Incorrect email or password.",+ "invalidVerificationToken": "Invalid verification token."}diff --git a/src/i18n/en/validation.json b/src/i18n/en/validation.jsonindex 5f1b15854..097d29c02 100644--- a/src/i18n/en/validation.json+++ b/src/i18n/en/validation.json@@ -2,5 +2,6 @@"isEmail": "Please enter the valid email.","isPassword": "Please enter the valid password.","isNotEmpty": "This field is required.",- "isBoolean": "Please enter the valid value (true or false)."+ "isBoolean": "Please enter the valid value (true or false).",+ "isUuid": "Please enter the valid UUID."}diff --git a/src/user/user.service.ts b/src/user/user.service.tsindex 747639d00..a6097d69c 100644--- a/src/user/user.service.ts+++ b/src/user/user.service.ts@@ -150,4 +150,8 @@ export class UserService {public async findUserById(id: string) {return await this.usersRepository.findOne({ id })}++ public async updateUser(id: string, user: Partial<Omit<User, 'id'>>) {+ return await this.usersRepository.update({ id }, user)+ }}