import { auth } from '@/lib/auth';
import type { AppEnv } from '@/types/hono';
import { createMiddleware } from 'hono/factory';

export const requireAuth = createMiddleware<AppEnv>(async (c, next) => {
  const session = await auth.api.getSession({
    headers: c.req.raw.headers,
  });

  if (!session) {
    return c.json({ message: 'Unauthorized' }, 401);
  }

  c.set('user', session.user);
  c.set('session', session.session);

  await next();
});
