feat(auth): 登录态改为滑动续期(后台 24h/7d,打手 7d/30d)
This commit is contained in:
@@ -60,16 +60,47 @@ export async function getActiveAdminSession(
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function touchAdminSession(sessionId: string, now: string): Promise<boolean> {
|
||||
const result = await query(
|
||||
/** 活跃请求的写入节流:last_seen_at 与滑动过期最多 5 分钟刷新一次。 */
|
||||
const ADMIN_SESSION_TOUCH_MIN_INTERVAL = '5 minutes'
|
||||
|
||||
/**
|
||||
* 校验会话有效并滑动续期。
|
||||
* 返回续期后的空闲到期时间;会话无效/已过期返回 null。
|
||||
*/
|
||||
export async function touchAdminSession(input: {
|
||||
sessionId: string
|
||||
now: string
|
||||
idleExpiresAt: string
|
||||
absoluteExpiresAt: string
|
||||
}): Promise<string | null> {
|
||||
const result = await query<{ expires_at: string | Date }>(
|
||||
`
|
||||
UPDATE admin_sessions
|
||||
SET last_seen_at = $2, updated_at = $2
|
||||
WHERE session_id = $1 AND status = 'active' AND expires_at > $2
|
||||
WITH target AS (
|
||||
SELECT session_id, expires_at
|
||||
FROM admin_sessions
|
||||
WHERE session_id = $1 AND status = 'active' AND expires_at > $2
|
||||
), touched AS (
|
||||
UPDATE admin_sessions s
|
||||
SET last_seen_at = $2,
|
||||
updated_at = $2,
|
||||
expires_at = LEAST($3::timestamptz, $4::timestamptz)
|
||||
FROM target
|
||||
WHERE s.session_id = target.session_id
|
||||
AND s.last_seen_at < ($2::timestamptz - INTERVAL '${ADMIN_SESSION_TOUCH_MIN_INTERVAL}')
|
||||
AND s.expires_at < LEAST($3::timestamptz, $4::timestamptz)
|
||||
RETURNING s.expires_at
|
||||
)
|
||||
SELECT COALESCE(
|
||||
(SELECT expires_at FROM touched),
|
||||
(SELECT expires_at FROM target)
|
||||
) AS expires_at
|
||||
`,
|
||||
[sessionId, now],
|
||||
[input.sessionId, input.now, input.idleExpiresAt, input.absoluteExpiresAt],
|
||||
)
|
||||
return result.rowCount === 1
|
||||
const expiresAt = result.rows[0]?.expires_at
|
||||
if (!expiresAt) return null
|
||||
const parsed = new Date(expiresAt).getTime()
|
||||
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : null
|
||||
}
|
||||
|
||||
export async function revokeAdminSession(sessionId: string, now: string): Promise<boolean> {
|
||||
|
||||
Reference in New Issue
Block a user