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 a route to create a password reset request

Yevhenii Kulisidicb789d047423
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index 54322ff31..88f4666df 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -30,6 +30,7 @@ import {
LoginResponseDto,
LoginRequestDto,
VerifyEmailDto,
+ RequestResetPasswordDto,
} from './dtos'
@ApiTags('Auth')
@@ -145,4 +146,30 @@ export class AuthController {
await this.authService.verifyEmail(actionToken)
}
+
+ @ApiOperation({ summary: 'Request a password reset' })
+ @ApiOkResponse({
+ description: 'Password reset requested',
+ })
+ @Post('reset-password')
+ public async requestResetPassword(
+ @Body() body: RequestResetPasswordDto,
+ @I18n() i18n: I18nContext,
+ @Headers() headers: unknown,
+ @Ip() requestIp: string,
+ ): Promise<void> {
+ const ip =
+ headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp
+
+ await checkRateLimit(ip, 'reset-password')
+ await checkRateLimit(body.email, 'reset-password')
+
+ const user = await this.userService.findUser(body.email)
+
+ if (!user) {
+ throw new ConflictException(i18n.t('auth.accountNotExists'))
+ }
+
+ await this.authService.sendResetPasswordEmail(user.id, user.email)
+ }
}
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index 3b2ada6ce..ac4bc7a8a 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -215,4 +215,23 @@ export class AuthService {
})
await this.actionTokensService.deleteActionToken(actionToken.id)
}
+
+ public async sendResetPasswordEmail(userId: string, email: string) {
+ const actionToken = await this.actionTokensService.createActionToken(
+ userId,
+ ActionTokenType.PASSWORD_RESET,
+ )
+
+ const verificationLink = `${this.configService.get(
+ 'CLIENT_URL',
+ )}/reset-password/${actionToken.id}`
+
+ await this.mailerService.sendEmail(
+ email,
+ LetterTemplate.ConfirmPasswordChange,
+ {
+ url: verificationLink,
+ },
+ )
+ }
}
diff --git a/src/auth/dtos/index.ts b/src/auth/dtos/index.ts
index db1d60442..bc80b38af 100644
--- a/src/auth/dtos/index.ts
+++ b/src/auth/dtos/index.ts
@@ -1,3 +1,4 @@
export * from './register.dto'
export * from './login.dto'
export * from './verify-email.dto'
+export * from './request-reset-password.dto'
diff --git a/src/auth/dtos/request-reset-password.dto.ts b/src/auth/dtos/request-reset-password.dto.ts
new file mode 100644
index 000000000..462874a54
--- /dev/null
+++ b/src/auth/dtos/request-reset-password.dto.ts
@@ -0,0 +1,13 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsEmail } from 'class-validator'
+
+export class RequestResetPasswordDto {
+ @ApiProperty({
+ description: 'User email',
+ example: '[email protected]',
+ maxLength: 254,
+ minLength: 6,
+ })
+ @IsEmail({}, { message: 'validation.isEmail' })
+ public readonly email: string
+}
diff --git a/src/auth/dtos/verify-email.dto.ts b/src/auth/dtos/verify-email.dto.ts
index 4a0c2e55e..96f24eaea 100644
--- a/src/auth/dtos/verify-email.dto.ts
+++ b/src/auth/dtos/verify-email.dto.ts
@@ -7,5 +7,5 @@ export class VerifyEmailDto {
example: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6',
})
@IsUUID('4', { message: 'validation.isUuid' })
- token: string
+ public readonly token: string
}
diff --git a/src/i18n/en/auth.json b/src/i18n/en/auth.json
index 48822e5d8..c504efcb1 100644
--- a/src/i18n/en/auth.json
+++ b/src/i18n/en/auth.json
@@ -1,5 +1,6 @@
{
"leakedPassword": "The password is in the leak database.",
"invalidCredentials": "Incorrect email or password.",
- "invalidVerificationToken": "Invalid verification token."
+ "invalidVerificationToken": "Invalid verification token.",
+ "accountNotExists": "An account with this email does not exist."
}