feat(dcauth): 新增后台手机号与设备认证登录
登录链路改为账号密码 + 短信验证码 + DCAuth 设备签名,并新增后台手机号绑定、 登录日志、设备绑定与重置接口。
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import { query } from '../db/client.js'
|
||||
|
||||
export type AdminSmsCodeRow = {
|
||||
id: number
|
||||
user_id: number
|
||||
phone: string
|
||||
purpose: string
|
||||
code_hash: string
|
||||
expires_at: string
|
||||
consumed_at: string | null
|
||||
failed_attempt_count: number
|
||||
request_ip: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export async function createAdminSmsCode(input: {
|
||||
userId: number
|
||||
phone: string
|
||||
purpose: string
|
||||
codeHash: string
|
||||
expiresAt: string
|
||||
requestIp: string
|
||||
now: string
|
||||
}): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
INSERT INTO admin_sms_codes (
|
||||
user_id, phone, purpose, code_hash, expires_at, consumed_at,
|
||||
failed_attempt_count, request_ip, created_at
|
||||
) VALUES ($1, $2, $3, $4, $5, NULL, 0, $6, $7)
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.userId,
|
||||
input.phone,
|
||||
input.purpose,
|
||||
input.codeHash,
|
||||
input.expiresAt,
|
||||
input.requestIp,
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function getLatestAdminSmsCode(
|
||||
userId: number,
|
||||
purpose: string,
|
||||
): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
SELECT *
|
||||
FROM admin_sms_codes
|
||||
WHERE user_id = $1 AND purpose = $2
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`,
|
||||
[userId, purpose],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function consumeAdminSmsCode(
|
||||
codeId: number,
|
||||
now: string,
|
||||
): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
UPDATE admin_sms_codes
|
||||
SET consumed_at = $1
|
||||
WHERE id = $2 AND consumed_at IS NULL
|
||||
RETURNING *
|
||||
`,
|
||||
[now, codeId],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function recordAdminSmsCodeFailure(
|
||||
codeId: number,
|
||||
now: string,
|
||||
maxAttempts: number,
|
||||
): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
UPDATE admin_sms_codes
|
||||
SET
|
||||
failed_attempt_count = COALESCE(failed_attempt_count, 0) + 1,
|
||||
consumed_at = CASE
|
||||
WHEN COALESCE(failed_attempt_count, 0) + 1 >= $3 THEN $2
|
||||
ELSE consumed_at
|
||||
END
|
||||
WHERE id = $1 AND consumed_at IS NULL
|
||||
RETURNING *
|
||||
`,
|
||||
[codeId, now, Math.max(1, Math.floor(maxAttempts))],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function countAdminSmsCodesOnDay(
|
||||
userId: number,
|
||||
dayStartIso: string,
|
||||
dayEndIso: string,
|
||||
): Promise<number> {
|
||||
const result = await query<{ total: number }>(
|
||||
`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM admin_sms_codes
|
||||
WHERE user_id = $1
|
||||
AND created_at >= $2
|
||||
AND created_at < $3
|
||||
`,
|
||||
[userId, dayStartIso, dayEndIso],
|
||||
)
|
||||
return Number(result.rows[0]?.total || 0)
|
||||
}
|
||||
Reference in New Issue
Block a user