add auth token cleanup logic

This commit is contained in:
2025-05-21 23:27:17 +01:00
parent 2c292db0fc
commit 255acfcb32
5 changed files with 69 additions and 29 deletions

View File

@@ -119,6 +119,13 @@ async function verifyAuthToken(cookies: Bun.CookieMap): Promise<User | null> {
return user
}
function startBackgroundAuthTokenCleanup() {
const query = db.query("DELETE FROM auth_tokens WHERE expires_at_unix_ms < $time")
setInterval(() => {
query.run({ time: new Date().valueOf() })
}, 5000)
}
async function signUp(request: Bun.BunRequest<"/api/sign-up">) {
const body = await request.json().catch(() => {
throw new HttpError(500)
@@ -199,4 +206,4 @@ async function logout(request: Bun.BunRequest<"/api/logout">, user: User): Promi
return new Response(undefined, { status: 204 })
}
export { authenticated, signUp, login, logout }
export { authenticated, signUp, login, logout, startBackgroundAuthTokenCleanup }