Skip to content

Commit f91297f

Browse files
committed
fix(auth): reuse cache key constant and handle already-verified reset codes
1 parent fc07bef commit f91297f

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/auth/application/use-cases/password/confirm-reset-password.use-case.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RESET_PASSWORD_CACHE_KEY } from '@core/auth/infrastructure/constants';
12
import { UpdatePasswordUseCase } from '@core/user';
23
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
34
import { CACHE_SERVICE } from '@shared/adapters/cache/constants';
@@ -17,7 +18,7 @@ export class ConfirmResetPasswordUseCase {
1718
) {}
1819

1920
async execute(dto: PasswordResetConfirmDto) {
20-
const redisKey = `pass:reset:${dto.email}`;
21+
const redisKey = RESET_PASSWORD_CACHE_KEY(dto.email);
2122
const cachedData = await this.cacheService.getOne(redisKey);
2223

2324
if (!cachedData) {

src/auth/application/use-cases/password/verify-reset-password.use-case.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RESET_PASSWORD_CACHE_KEY } from '@core/auth/infrastructure/constants';
12
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
23
import { CACHE_SERVICE } from '@shared/adapters/cache/constants';
34
import { ICacheService } from '@shared/adapters/cache/ports';
@@ -15,7 +16,7 @@ export class VerifyResetPasswordUseCase {
1516
) {}
1617

1718
async execute(dto: VerifyResetCodeDto) {
18-
const redisKey = `pass:reset:${dto.email}`;
19+
const redisKey = RESET_PASSWORD_CACHE_KEY(dto.email);
1920
const cachedData = await this.cacheService.getOne(redisKey);
2021

2122
if (!cachedData) {
@@ -43,14 +44,14 @@ export class VerifyResetPasswordUseCase {
4344
);
4445
}
4546

46-
if (!resetSession.isVerified) {
47+
if (resetSession.isVerified) {
4748
throw new BaseException(
4849
{
49-
code: AuthErrorCodes.CODE_NOT_VERIFIED,
50-
message: AuthErrorMessages[AuthErrorCodes.CODE_NOT_VERIFIED],
51-
details: [{ target: 'code', message: 'Код подтверждения не верифицирован' }],
50+
code: AuthErrorCodes.CODE_ALREADY_VERIFIED,
51+
message: AuthErrorMessages[AuthErrorCodes.CODE_ALREADY_VERIFIED],
52+
details: [{ target: 'code', message: 'Код подтверждения уже верифицирован' }],
5253
},
53-
HttpStatus.FORBIDDEN,
54+
HttpStatus.BAD_REQUEST,
5455
);
5556
}
5657

src/auth/domain/errors/auth.error.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const AuthErrorCodes = {
1010
TOKEN_INVALID: 'AUTH.TOKEN_INVALID',
1111
REFRESH_FAILED: 'AUTH.REFRESH_FAILED',
1212
CODE_NOT_VERIFIED: 'AUTH.CODE_NOT_VERIFIED',
13+
CODE_ALREADY_VERIFIED: 'AUTH.CODE_ALREADY_VERIFIED',
1314
SIGNUP_FAILED: 'AUTH.SIGNUP_FAILED',
1415
UNAUTHORIZED: 'AUTH.UNAUTHORIZED',
1516
RESET_SESSION_NOT_FOUND: 'AUTH.RESET_SESSION_NOT_FOUND',
@@ -39,6 +40,7 @@ export const AuthErrorMessages: Record<AuthErrorCode, string> = {
3940
[AuthErrorCodes.SESSION_REVOKED]: 'Сессия была отозвана. Выполните вход заново',
4041
[AuthErrorCodes.STRATEGY_NOT_FOUND]: 'Неизвестный контекст операции',
4142
[AuthErrorCodes.CODE_NOT_VERIFIED]: 'Код подтверждения еще не был верифицирован',
43+
[AuthErrorCodes.CODE_ALREADY_VERIFIED]: 'Код подтверждения уже верифицирован',
4244
[AuthErrorCodes.UNAUTHORIZED]: 'Необходимо выполнить вход',
4345
[AuthErrorCodes.RESET_SESSION_NOT_FOUND]: 'Запрос на сброс пароля не найден',
4446
[AuthErrorCodes.REGISTRATION_EXPIRED]: 'Регистрация истекла. Зарегистрируйтесь заново',

0 commit comments

Comments
 (0)