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 reset password

Yevhenii Kulisidi3a624ff315cb
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index 88f4666df..522fa034c 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -31,6 +31,8 @@ import {
LoginRequestDto,
VerifyEmailDto,
RequestResetPasswordDto,
+ ConfirmResetPasswordDto,
+ ResetPasswordDto,
} from './dtos'
@ApiTags('Auth')
@@ -172,4 +174,26 @@ export class AuthController {
await this.authService.sendResetPasswordEmail(user.id, user.email)
}
+
+ @ApiOperation({ summary: 'Reset a password' })
+ @ApiOkResponse({
+ description: 'Password reset',
+ })
+ @Post('reset-password/confirm/:token')
+ @HttpCode(200)
+ public async resetPassword(
+ @Param() params: ConfirmResetPasswordDto,
+ @Body() body: ResetPasswordDto,
+ @I18n() i18n: I18nContext,
+ ): Promise<void> {
+ const actionToken = await this.authService.checkResetPasswordToken(
+ params.token,
+ )
+
+ if (!actionToken) {
+ throw new ConflictException(i18n.t('auth.invalidResetPasswordToken'))
+ }
+
+ await this.authService.resetPassword(actionToken, body.newPassword)
+ }
}
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index ac4bc7a8a..44ed1a6cd 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -234,4 +234,28 @@ export class AuthService {
},
)
}
+
+ public async checkResetPasswordToken(
+ token: string,
+ ): Promise<ActionToken | null> {
+ const actionToken = await this.actionTokensService.findActionToken(token)
+
+ if (actionToken && actionToken.action === ActionTokenType.PASSWORD_RESET) {
+ return actionToken
+ }
+
+ return null
+ }
+
+ public async resetPassword(
+ actionToken: ActionToken,
+ password: string,
+ ): Promise<void> {
+ const hashedPassword = await this.hashPassword(password)
+
+ await this.userService.updateUser(actionToken.user.id, {
+ password: hashedPassword,
+ })
+ await this.actionTokensService.deleteActionToken(actionToken.id)
+ }
}
diff --git a/src/auth/dtos/confirm-reset-password.dto.ts b/src/auth/dtos/confirm-reset-password.dto.ts
new file mode 100644
index 000000000..4d367f1f6
--- /dev/null
+++ b/src/auth/dtos/confirm-reset-password.dto.ts
@@ -0,0 +1,11 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsUUID } from 'class-validator'
+
+export class ConfirmResetPasswordDto {
+ @ApiProperty({
+ description: 'The token to confirm the reset password',
+ 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.ts
index bc80b38af..f857073ec 100644
--- a/src/auth/dtos/index.ts
+++ b/src/auth/dtos/index.ts
@@ -2,3 +2,5 @@ export * from './register.dto'
export * from './login.dto'
export * from './verify-email.dto'
export * from './request-reset-password.dto'
+export * from './confirm-reset-password.dto'
+export * from './reset-password.dto'
diff --git a/src/auth/dtos/register.dto.ts b/src/auth/dtos/register.dto.ts
index 936e7c226..3ce6ca9dc 100644
--- a/src/auth/dtos/register.dto.ts
+++ b/src/auth/dtos/register.dto.ts
@@ -9,7 +9,7 @@ export class RegisterRequestDto {
maxLength: 254,
minLength: 6,
})
- @IsEmail({}, { message: 'validation.isEmail' })
+ @IsEmail({}, { message: 'Please enter the valid email.' })
public readonly email: string
@ApiProperty({
@@ -18,15 +18,15 @@ export class RegisterRequestDto {
maxLength: 72,
minLength: 8,
})
- @IsPassword({ message: 'validation.isPassword' })
+ @IsPassword({ message: 'Please enter the valid password.' })
public readonly password: string
@ApiProperty({
description: 'Check if password is leaked',
example: true,
})
- @IsNotEmpty({ message: 'validation.isNotEmpty' })
- @IsBoolean({ message: 'validation.isBoolean' })
+ @IsNotEmpty({ message: 'This field is required.' })
+ @IsBoolean({ message: 'Please enter the valid value (true or false).' })
public readonly checkIfLeaked: boolean
}
diff --git a/src/auth/dtos/request-reset-password.dto.ts b/src/auth/dtos/request-reset-password.dto.ts
index 462874a54..a2a09a383 100644
--- a/src/auth/dtos/request-reset-password.dto.ts
+++ b/src/auth/dtos/request-reset-password.dto.ts
@@ -8,6 +8,6 @@ export class RequestResetPasswordDto {
maxLength: 254,
minLength: 6,
})
- @IsEmail({}, { message: 'validation.isEmail' })
+ @IsEmail({}, { message: 'Please enter the valid email.' })
public readonly email: string
}
diff --git a/src/auth/dtos/reset-password.dto.ts b/src/auth/dtos/reset-password.dto.ts
new file mode 100644
index 000000000..d6cc41a23
--- /dev/null
+++ b/src/auth/dtos/reset-password.dto.ts
@@ -0,0 +1,13 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsPassword } from '../decorators'
+
+export class ResetPasswordDto {
+ @ApiProperty({
+ description: 'User password',
+ example: '%d7*c4W45p',
+ maxLength: 72,
+ minLength: 8,
+ })
+ @IsPassword({ message: 'Please enter the valid password.' })
+ public readonly newPassword: string
+}
diff --git a/src/auth/dtos/verify-email.dto.ts b/src/auth/dtos/verify-email.dto.ts
index 96f24eaea..e03420979 100644
--- a/src/auth/dtos/verify-email.dto.ts
+++ b/src/auth/dtos/verify-email.dto.ts
@@ -6,6 +6,6 @@ export class VerifyEmailDto {
description: 'The token to verify the email',
example: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6',
})
- @IsUUID('4', { message: 'validation.isUuid' })
+ @IsUUID('4', { message: 'Please enter the valid UUID.' })
public readonly token: string
}
diff --git a/src/i18n/en/auth.json b/src/i18n/en/auth.json
index c504efcb1..93f9842fe 100644
--- a/src/i18n/en/auth.json
+++ b/src/i18n/en/auth.json
@@ -2,5 +2,6 @@
"leakedPassword": "The password is in the leak database.",
"invalidCredentials": "Incorrect email or password.",
"invalidVerificationToken": "Invalid verification token.",
- "accountNotExists": "An account with this email does not exist."
+ "accountNotExists": "An account with this email does not exist.",
+ "invalidResetPasswordToken": "Invalid reset password token."
}