1
0
forked from Yara724/api
This commit is contained in:
SepehrYahyaee
2026-05-24 10:10:17 +03:30
parent 91221a6848
commit af875a4773
20 changed files with 439 additions and 9 deletions

View File

@@ -0,0 +1,8 @@
import { Module } from "@nestjs/common";
import { CaptchaService } from "./captcha.service";
@Module({
providers: [CaptchaService],
exports: [CaptchaService],
})
export class CaptchaModule {}

View File

@@ -0,0 +1,42 @@
import { Injectable } from "@nestjs/common";
import * as svgCaptcha from "svg-captcha";
export interface GeneratedCaptcha {
text: string;
image: string;
expiresAt: number;
}
@Injectable()
export class CaptchaService {
generate(): GeneratedCaptcha {
const captcha = svgCaptcha.create({
size: 5,
ignoreChars: "0oO1ilI",
noise: 3,
color: true,
background: "#f8fafc",
width: 160,
height: 56,
fontSize: 52,
});
return {
text: captcha.text,
image: `data:image/svg+xml;base64,${Buffer.from(captcha.data, "utf8").toString("base64")}`,
expiresAt: this.buildExpireAt(),
};
}
normalizeAnswer(input: string): string {
return input.trim().toLowerCase();
}
buildExpireAt(): number {
const raw = Number(
process.env.EXP_CAPTCHA_TIME ?? process.env.EXP_OTP_TIME ?? "2",
);
const minutes = Number.isFinite(raw) && raw > 0 ? raw : 2;
return Date.now() + minutes * 60 * 1000;
}
}