Fixed captcha for prod

This commit is contained in:
SepehrYahyaee
2026-05-31 16:15:55 +03:30
parent 02a69f3db2
commit e90c6a5c50
2 changed files with 14 additions and 32 deletions

View File

@@ -12,49 +12,40 @@ import { HashService } from "src/utils/hash/hash.service";
@Injectable()
export class CaptchaChallengeService {
private readonly isDev: boolean;
constructor(
private readonly captchaService: CaptchaService,
private readonly hashService: HashService,
private readonly captchaChallengeDbService: CaptchaChallengeDbService,
private readonly configService: ConfigService,
) {}
) {
this.isDev = this.configService.get<string>("NODE_ENV") === "development";
}
async issue(): Promise<CaptchaResponseDto> {
const generated = this.captchaService.generate();
const captchaId = randomUUID();
if (this.configService.get<string>("NODE_ENV") === "development") {
const answerHash = await this.hashService.hash(
this.captchaService.normalizeAnswer(generated.text),
);
const result = {
captchaId,
answerHash:
this.configService.get<string>("NODE_ENV") === "development"
? answerHash
: "",
image: generated.image,
expiresAt: generated.expiresAt,
expireAt: new Date(generated.expiresAt),
usedAt: null,
};
}
// Always hash and persist the answer — the env check was the root bug
const answerHash = await this.hashService.hash(
this.captchaService.normalizeAnswer(generated.text),
);
const result = {
await this.captchaChallengeDbService.create({
captchaId,
answerHash,
image: generated.image,
expiresAt: generated.expiresAt,
expireAt: new Date(generated.expiresAt),
usedAt: null,
};
await this.captchaChallengeDbService.create(result);
});
return {
captchaId,
text: generated.text /* TODO: REMOVE THIS FOR PROD*/,
image: generated.image,
expiresAt: generated.expiresAt,
...(this.isDev && { text: generated.text }),
};
}