// Legt den Plattform-Betreiber (Rolle superadmin, ohne Firma) an. // Aufruf: SUPABASE_URL=http://localhost:8000 SUPABASE_SERVICE_ROLE_KEY=... \ // node scripts/superadmin-anlegen.mjs // Nur fetch, keine Abhaengigkeiten: laeuft auch ausserhalb von web/node_modules. const [email, passwort, name] = process.argv.slice(2); const url = process.env.SUPABASE_URL; const key = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!email || !passwort || !name || !url || !key) { console.error("Aufruf: SUPABASE_URL=... SUPABASE_SERVICE_ROLE_KEY=... node scripts/superadmin-anlegen.mjs "); process.exit(1); } const headers = { apikey: key, Authorization: `Bearer ${key}`, "Content-Type": "application/json" }; const antwort = await fetch(`${url}/auth/v1/admin/users`, { method: "POST", headers, body: JSON.stringify({ email, password: passwort, email_confirm: true, user_metadata: { name } }), }); const nutzer = await antwort.json(); if (!antwort.ok) { console.error("Nutzer anlegen fehlgeschlagen:", nutzer); process.exit(1); } const profil = await fetch(`${url}/rest/v1/profiles?id=eq.${nutzer.id}`, { method: "PATCH", headers: { ...headers, Prefer: "return=representation" }, body: JSON.stringify({ role: "superadmin" }), }); const zeilen = await profil.json(); if (!profil.ok || !Array.isArray(zeilen) || zeilen.length !== 1) { console.error("Rolle setzen fehlgeschlagen, Nutzer wird wieder geloescht:", zeilen); await fetch(`${url}/auth/v1/admin/users/${nutzer.id}`, { method: "DELETE", headers }); process.exit(1); } console.log(`Superadmin angelegt: ${email} (${nutzer.id})`);