import { query } from '../db.js';
export async function sessionAuth(c, next) {
    const authHeader = c.req.header('Authorization');
    if (!authHeader?.startsWith('Bearer ')) {
        return c.json({ error: 'Missing or invalid Authorization header' }, 401);
    }
    const token = authHeader.slice(7);
    const result = query(`SELECT s.token, s.user_id, s.expires_at, s.created_at, u.email
     FROM sessions s JOIN users u ON s.user_id = u.id
     WHERE s.token = ? AND s.expires_at > datetime('now')`, [token]);
    if (result.rows.length === 0) {
        return c.json({ error: 'Invalid or expired session' }, 401);
    }
    const session = result.rows[0];
    c.set('userId', session.user_id);
    c.set('userEmail', session.email);
    c.set('sessionToken', session.token);
    await next();
}
//# sourceMappingURL=session.js.map