암호 키 등록 챌린지 처리 방식을 세션 업그레이드 챌린지 처리 방식과 동일하게 변경

This commit is contained in:
static
2025-01-12 07:59:49 +09:00
parent 1a86c8d9e0
commit be8587694e
3 changed files with 36 additions and 31 deletions

View File

@@ -119,26 +119,21 @@ export const registerUserClientChallenge = async (
});
};
export const getUserClientChallenge = async (answer: string, ip: string) => {
export const consumeUserClientChallenge = async (userId: number, answer: string, ip: string) => {
const challenges = await db
.select()
.from(userClientChallenge)
.delete(userClientChallenge)
.where(
and(
eq(userClientChallenge.userId, userId),
eq(userClientChallenge.answer, answer),
eq(userClientChallenge.allowedIp, ip),
gt(userClientChallenge.expiresAt, new Date()),
eq(userClientChallenge.isUsed, false),
),
)
.limit(1);
.returning({ clientId: userClientChallenge.clientId });
return challenges[0] ?? null;
};
export const markUserClientChallengeAsUsed = async (id: number) => {
await db.update(userClientChallenge).set({ isUsed: true }).where(eq(userClientChallenge.id, id));
};
export const cleanupExpiredUserClientChallenges = async () => {
await db.delete(userClientChallenge).where(lte(userClientChallenge.expiresAt, new Date()));
};