import { c as createError, Q as useSession } from '../nitro/nitro.mjs';
import { g as getUser } from './getUser.mjs';

async function setSession(event, user) {
  const session = await useSession(event, { password: process.env.NUXT_SESSION_PASSWORD, name: "nuxt-session" });
  await session.update(user);
  return session.data;
}
async function requireUser(event) {
  const user = await getUser(event);
  if (!user) {
    throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
  }
  return user;
}
async function requireSessionRole(evt, roles) {
  const user = await isSessionRole(evt, roles);
  if (!user) {
    throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
  }
  return user;
}
async function isSessionRole(evt, roles) {
  const user = await getUser(evt);
  if (!user) return;
  if (!Array.isArray(roles)) {
    roles = [roles];
  }
  return roles.includes(user?.role) ? user : false;
}
async function requireAdminSession(evt) {
  return requireSessionRole(evt, "admin");
}

export { requireSessionRole as a, requireUser as b, isSessionRole as i, requireAdminSession as r, setSession as s };
