import { d as defineEventHandler, r as requireAuth, c as createError, G as createContentManager, du as generateJWT, $ as getAuthRequestContext, dG as emitAuthTokenRefresh } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const refresh_post = defineEventHandler(async (event) => {
  await requireAuth(event);
  if (event.context.apiToken) {
    throw createError({
      statusCode: 403,
      statusMessage: "Forbidden",
      message: "API tokens cannot be exchanged for user sessions"
    });
  }
  if (event.context.adminUser) {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Use /api/admin/auth/refresh for admin users"
    });
  }
  const userCtx = event.context.user;
  if (!userCtx) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: "Authentication required"
    });
  }
  const userManager = createContentManager("user");
  const user = await userManager.findOne(userCtx.id, { populate: "role" });
  if (!user) {
    throw createError({
      statusCode: 401,
      statusMessage: "Unauthorized",
      message: "User not found"
    });
  }
  if (user.blocked) {
    throw createError({
      statusCode: 403,
      statusMessage: "Forbidden",
      message: "User is blocked"
    });
  }
  const roleObj = user.role;
  const roleName = (roleObj == null ? void 0 : roleObj.name) || userCtx.role || "authenticated";
  const username = user.username || userCtx.username || "";
  const email = user.email || userCtx.email || "";
  const token = generateJWT({
    id: userCtx.id,
    username,
    email,
    role: roleName
  });
  const reqCtx = getAuthRequestContext(event);
  await emitAuthTokenRefresh(userCtx.id, {
    ...reqCtx,
    username,
    email,
    role: roleName
  });
  return {
    success: true,
    data: {
      token,
      user: {
        id: userCtx.id,
        username,
        email,
        role: roleName
      }
    }
  };
});

export { refresh_post as default };
//# sourceMappingURL=refresh.post.mjs.map
