UNPKG

codalware-auth

Version:

Complete authentication system with enterprise security, attack protection, team workspaces, waitlist, billing, UI components, 2FA, and account recovery - production-ready in 5 minutes. Enhanced CLI with verification, rollback, and App Router scaffolding.

102 lines (85 loc) 2.75 kB
#!/usr/bin/env node /* Create or update a SUPER_ADMIN user in the database. Usage: ADMIN_EMAIL=admin@example.com ADMIN_PASSWORD=pass node scripts/create-super-admin.js This script will upsert a user with the given email and password and set role = 'SUPER_ADMIN'. */ import path from 'path'; import fs from 'fs'; import dotenv from 'dotenv'; import { PrismaClient } from '@prisma/client'; import bcrypt from 'bcryptjs'; const loadEnvFiles = () => { const envFiles = ['.env.local', '.env']; for (const file of envFiles) { const envPath = path.join(process.cwd(), file); if (fs.existsSync(envPath)) { dotenv.config({ path: envPath, override: false }); } } }; const loadConfigEnv = async () => { const configCandidates = [ path.join(process.cwd(), 'config', 'env.js'), path.join(process.cwd(), 'config', 'env.cjs') ]; for (const candidate of configCandidates) { if (fs.existsSync(candidate)) { try { // dynamic import to load user config file without forcing CJS/ESM assumptions const mod = await import(candidate); // prefer default export if present return mod?.default ?? mod; } catch (error) { if (process.env.DEBUG) { console.error('Failed to load custom config env:', error); } } } } return {}; }; loadEnvFiles(); const configEnv = await loadConfigEnv(); const ADMIN_EMAIL = process.env.ADMIN_EMAIL || configEnv.ADMIN_EMAIL; const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || configEnv.ADMIN_PASSWORD; const ADMIN_NAME = process.env.ADMIN_NAME || configEnv.ADMIN_NAME; const prisma = new PrismaClient(); async function main() { const email = ADMIN_EMAIL; const password = ADMIN_PASSWORD; const name = ADMIN_NAME || 'Super Admin'; if (!email || !password) { console.error('Missing ADMIN_EMAIL or ADMIN_PASSWORD environment variables.'); console.error('Example: ADMIN_EMAIL=admin@example.com ADMIN_PASSWORD=Pa$$w0rd node scripts/create-super-admin.js'); process.exit(1); } const hashed = await bcrypt.hash(password, 10); const user = await prisma.user.upsert({ where: { email }, update: { name, password: hashed, role: 'SUPER_ADMIN', status: 'APPROVED', emailVerified: new Date(), }, create: { email, name, password: hashed, role: 'SUPER_ADMIN', status: 'APPROVED', emailVerified: new Date(), }, }); console.log(`Super admin upserted: ${user.email} (id=${user.id})`); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });