Bind Better Auth sessions to devices

This commit is contained in:
2026-05-09 03:46:15 -04:00
parent 0c64a7721e
commit 2dda91c50b
6 changed files with 88 additions and 6 deletions
+30
View File
@@ -166,6 +166,24 @@ const DEVICE_REVOKE_QUERY = `
revoked_at = COALESCE(revoked_at, ?)
WHERE user_id = ? AND device_id = ? AND revoked_at IS NULL
`;
const SESSION_DEVICE_CONTEXT_UPSERT_QUERY = `
INSERT INTO better_auth_session_device_context (
session_id,
user_id,
device_id,
updated_at
)
SELECT ?, ?, ?, ?
WHERE EXISTS (
SELECT 1
FROM better_auth_session
WHERE id = ? AND userId = ?
)
ON CONFLICT(session_id) DO UPDATE SET
user_id = excluded.user_id,
device_id = excluded.device_id,
updated_at = excluded.updated_at
`;
export async function deviceListDocument(
env: Env,
@@ -208,6 +226,7 @@ export async function registerDeviceDocument(
if (row === null) {
throw new DevicePersistenceError("device_registration_missing");
}
await bindSessionDeviceContext(env, context, registration.deviceId, nowSeconds);
return {
version: 1,
@@ -216,6 +235,17 @@ export async function registerDeviceDocument(
};
}
async function bindSessionDeviceContext(
env: Env,
context: AuthContext,
deviceId: string,
nowSeconds: number,
): Promise<void> {
await env.ELY_DB.prepare(SESSION_DEVICE_CONTEXT_UPSERT_QUERY)
.bind(context.sessionId, context.userId, deviceId, nowSeconds, context.sessionId, context.userId)
.run();
}
export async function approveDeviceDocument(
request: Request,
env: Env,