feat(auth): 登录态改为滑动续期(后台 24h/7d,打手 7d/30d)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { addHours } from './time.js'
|
||||
|
||||
export type SessionExpiryWindow = {
|
||||
/** 空闲窗口:每次访问可滑动续期到的目标时间。 */
|
||||
idleExpiresAt: string
|
||||
/** 绝对上限:从登录起算,任何续期都不能突破。 */
|
||||
absoluteExpiresAt: string
|
||||
}
|
||||
|
||||
function normalizeHours(value: unknown, fallback: number): number {
|
||||
const parsed = Number(value)
|
||||
return Number.isFinite(parsed) && parsed >= 1 ? Math.floor(parsed) : fallback
|
||||
}
|
||||
|
||||
/** 登录时计算空闲到期与绝对到期;绝对上限小于空闲窗口时以绝对上限为准。 */
|
||||
export function resolveSessionExpiryWindow(input: {
|
||||
now: string
|
||||
idleHours: number
|
||||
absoluteHours: number
|
||||
}): SessionExpiryWindow {
|
||||
const requestedIdleHours = normalizeHours(input.idleHours, 24)
|
||||
const absoluteHours = normalizeHours(input.absoluteHours, requestedIdleHours)
|
||||
const idleHours = Math.min(requestedIdleHours, absoluteHours)
|
||||
return {
|
||||
idleExpiresAt: addHours(input.now, idleHours),
|
||||
absoluteExpiresAt: addHours(input.now, absoluteHours),
|
||||
}
|
||||
}
|
||||
|
||||
/** 滑动续期目标:now + 空闲窗口,但不得超过 Token 里的绝对上限。 */
|
||||
export function resolveSlidingSessionExpiresAt(input: {
|
||||
now: string
|
||||
idleHours: number
|
||||
absoluteExpiresAt: string
|
||||
}): string {
|
||||
const idleHours = normalizeHours(input.idleHours, 24)
|
||||
const target = addHours(input.now, idleHours)
|
||||
const absoluteMs = Date.parse(String(input.absoluteExpiresAt || ''))
|
||||
if (!Number.isFinite(absoluteMs)) return target
|
||||
return Date.parse(target) <= absoluteMs ? target : new Date(absoluteMs).toISOString()
|
||||
}
|
||||
Reference in New Issue
Block a user