forked from Yara724/api
FIX Catcha
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { Model } from "mongoose";
|
||||
import {
|
||||
CaptchaChallenge,
|
||||
CaptchaChallengeDocument,
|
||||
} from "../schema/captcha-challenge.schema";
|
||||
|
||||
@Injectable()
|
||||
export class CaptchaChallengeDbService {
|
||||
constructor(
|
||||
@InjectModel(CaptchaChallenge.name)
|
||||
private readonly captchaChallengeModel: Model<CaptchaChallengeDocument>,
|
||||
) {}
|
||||
|
||||
create(data: CaptchaChallenge): Promise<CaptchaChallengeDocument> {
|
||||
return this.captchaChallengeModel.create(data);
|
||||
}
|
||||
|
||||
findByCaptchaId(
|
||||
captchaId: string,
|
||||
): Promise<CaptchaChallengeDocument | null> {
|
||||
return this.captchaChallengeModel.findOne({ captchaId });
|
||||
}
|
||||
|
||||
markUsed(captchaId: string): Promise<CaptchaChallengeDocument | null> {
|
||||
return this.captchaChallengeModel.findOneAndUpdate(
|
||||
{ captchaId, usedAt: null },
|
||||
{ $set: { usedAt: new Date() } },
|
||||
{ new: true },
|
||||
);
|
||||
}
|
||||
}
|
||||
28
src/captcha/entities/schema/captcha-challenge.schema.ts
Normal file
28
src/captcha/entities/schema/captcha-challenge.schema.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
|
||||
import { HydratedDocument } from "mongoose";
|
||||
|
||||
@Schema({ collection: "captcha-challenges", timestamps: true, versionKey: false })
|
||||
export class CaptchaChallenge {
|
||||
@Prop({ required: true, unique: true, index: true })
|
||||
captchaId: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
answerHash: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
image: string;
|
||||
|
||||
@Prop({ required: true, index: true })
|
||||
expiresAt: number;
|
||||
|
||||
/** Mongo TTL — document removed shortly after this time. */
|
||||
@Prop({ required: true, expires: 0 })
|
||||
expireAt: Date;
|
||||
|
||||
@Prop({ default: null })
|
||||
usedAt?: Date | null;
|
||||
}
|
||||
|
||||
export type CaptchaChallengeDocument = HydratedDocument<CaptchaChallenge>;
|
||||
export const CaptchaChallengeSchema =
|
||||
SchemaFactory.createForClass(CaptchaChallenge);
|
||||
Reference in New Issue
Block a user