import { createHash } from 'node:crypto' import { findActiveClientByTokenHash, markClientUsed } from '~/data/repositories/clients' import type { ApiClient } from '~/data/schema' export function hashToken(token: string): string { return createHash('sha256').update(token).digest('hex') } export async function resolveClient(request: Request): Promise { const header = request.headers.get('authorization') if (!header?.startsWith('Bearer ')) { return undefined } const token = header.slice('Bearer '.length).trim() if (token.length === 0) { return undefined } const client = await findActiveClientByTokenHash(hashToken(token)) if (client) { await markClientUsed(client.id) } return client }