| #!/usr/bin/env node |
| |
| |
| |
| |
|
|
| import { createClient } from "@supabase/supabase-js"; |
| import { readFileSync } from "fs"; |
| import { fileURLToPath } from "url"; |
| import { dirname, join } from "path"; |
|
|
| const __filename = fileURLToPath(import.meta.url); |
| const __dirname = dirname(__filename); |
|
|
| |
| function loadEnv(path) { |
| try { |
| const content = readFileSync(path, "utf-8"); |
| const env = {}; |
| for (const line of content.split("\n")) { |
| const match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/); |
| if (match) { |
| const key = match[1]; |
| let value = match[2].trim(); |
| |
| if ((value.startsWith('"') && value.endsWith('"')) || |
| (value.startsWith("'") && value.endsWith("'"))) { |
| value = value.slice(1, -1); |
| } |
| env[key] = value; |
| } |
| } |
| return env; |
| } catch (e) { |
| return {}; |
| } |
| } |
|
|
| const env = loadEnv(join(__dirname, "../apps/web/.env.local")); |
| const supabaseUrl = env.NEXT_PUBLIC_SUPABASE_URL || process.env.NEXT_PUBLIC_SUPABASE_URL; |
| const supabaseServiceKey = env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SERVICE_ROLE_KEY; |
|
|
| if (!supabaseUrl || !supabaseServiceKey) { |
| console.error("Missing required environment variables:"); |
| console.error(" - NEXT_PUBLIC_SUPABASE_URL"); |
| console.error(" - SUPABASE_SERVICE_ROLE_KEY"); |
| process.exit(1); |
| } |
|
|
| const email = process.argv[2]; |
| if (!email) { |
| console.error("Usage: node scripts/set-superuser.mjs <user-email>"); |
| process.exit(1); |
| } |
|
|
| const supabase = createClient(supabaseUrl, supabaseServiceKey); |
|
|
| async function main() { |
| |
| const { data: { users }, error: listError } = await supabase.auth.admin.listUsers(); |
| |
| if (listError) { |
| console.error("Failed to list users:", listError.message); |
| process.exit(1); |
| } |
|
|
| const user = users.find((u) => u.email === email); |
| if (!user) { |
| console.error(`User with email "${email}" not found`); |
| process.exit(1); |
| } |
|
|
| console.log(`Found user: ${user.id} (${user.email})`); |
|
|
| |
| let { data: member } = await supabase |
| .from("members") |
| .select("*, organizations(id, name)") |
| .eq("user_id", user.id) |
| .single(); |
|
|
| if (!member) { |
| console.log("No member record found. Creating organization and membership..."); |
| |
| |
| const slug = `org-${user.id.slice(0, 8)}`; |
| const orgName = email.split("@")[0] || "Admin Organization"; |
| |
| const { data: org, error: orgErr } = await supabase |
| .from("organizations") |
| .insert({ name: orgName, slug }) |
| .select("id") |
| .single(); |
| |
| if (orgErr) { |
| console.error("Failed to create organization:", orgErr.message); |
| process.exit(1); |
| } |
| |
| console.log(`Created organization: ${org.id}`); |
| |
| |
| const { data: newMember, error: memberErr } = await supabase |
| .from("members") |
| .insert({ |
| organization_id: org.id, |
| user_id: user.id, |
| role: "owner", |
| is_superuser: true |
| }) |
| .select("*") |
| .single(); |
| |
| if (memberErr) { |
| console.error("Failed to create member:", memberErr.message); |
| process.exit(1); |
| } |
| |
| member = newMember; |
| console.log(`Created member record with superuser privileges`); |
| } |
|
|
| |
| if (member.is_superuser) { |
| console.log(`✓ ${email} is already a superuser`); |
| return; |
| } |
|
|
| const { error: updateError } = await supabase |
| .from("members") |
| .update({ is_superuser: true }) |
| .eq("user_id", user.id); |
|
|
| if (updateError) { |
| console.error("Failed to update member:", updateError.message); |
| process.exit(1); |
| } |
|
|
| console.log(`✓ Successfully set ${email} as superuser`); |
| console.log("You can now access /admin with this account"); |
| } |
|
|
| main().catch((err) => { |
| console.error("Unexpected error:", err); |
| process.exit(1); |
| }); |
|
|