create.js (820B)
1 import { app } from '../../../app.js'; 2 import { authenticateAdminSession } from '../../../lib/admin-auth.js'; 3 import { createAdminRealm } from '../../../lib/admin-realms.js'; 4 import { HttpError } from '../../../lib/http/http-error.js'; 5 import { canCreateRealm } from '../../../lib/policy.js'; 6 7 export default { 8 method: 'post', 9 path: '/api/v1/admin/realms', 10 name: 'admin.realms.create', 11 async handler(req) { 12 const auth = await authenticateAdminSession(req); 13 if (!canCreateRealm(auth.user)) { 14 throw new HttpError(403, 'forbidden', 'Realm creation access is required'); 15 } 16 17 const realm = await createAdminRealm({ 18 systemDb: auth.systemDb, 19 actor: auth.user, 20 body: req.body || {}, 21 }); 22 23 return new app.HttpOk({ ok: true, realm }, { 'cache-control': 'no-store' }); 24 }, 25 };