Add a exists check
This commit is contained in:
@@ -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(
|
||||||
|
|||||||
Reference in New Issue
Block a user