Add a exists check

This commit is contained in:
2026-01-22 15:06:26 -07:00
parent e2545a0f10
commit e164e4fdb0

View File

@@ -8,6 +8,7 @@ import {
} from '../lib/util' } from '../lib/util'
import { randomBytes } from 'crypto' import { randomBytes } from 'crypto'
import { verifyCodes } from '../lib/tables' import { verifyCodes } from '../lib/tables'
import { and, desc, eq, sql } from 'drizzle-orm'
type Body = { type Body = {
token: string token: string
@@ -49,6 +50,29 @@ export async function handler (context: Context) {
) )
const { connection: connection0, db: db0 } = dbInfo0 const { connection: connection0, db: db0 } = dbInfo0
const codeExists = await db0
.select({ code: verifyCodes.code })
.from(verifyCodes)
.where(
and(
eq(verifyCodes.ip, ip),
eq(verifyCodes.used, false),
sql`${verifyCodes.timestamp} >= UNIX_TIMESTAMP() - 600`
)
)
.orderBy(desc(verifyCodes.id))
.limit(1)
.execute()
if (codeExists[0])
return jsonResponse(
{
success: true,
message: null,
data: codeExists[0].code
},
200
)
await db0.insert(verifyCodes).values({ code, ip, timestamp: time }) await db0.insert(verifyCodes).values({ code, ip, timestamp: time })
return jsonResponse( return jsonResponse(