70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
|
|
/**
|
||
|
|
* Setup script to create initial admin user
|
||
|
|
* Run with: npx ts-node scripts/setup-admin.ts
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { PrismaClient } from '@prisma/client';
|
||
|
|
import bcrypt from 'bcryptjs';
|
||
|
|
import readline from 'readline';
|
||
|
|
|
||
|
|
const prisma = new PrismaClient();
|
||
|
|
|
||
|
|
const rl = readline.createInterface({
|
||
|
|
input: process.stdin,
|
||
|
|
output: process.stdout,
|
||
|
|
});
|
||
|
|
|
||
|
|
function question(query: string): Promise<string> {
|
||
|
|
return new Promise((resolve) => {
|
||
|
|
rl.question(query, resolve);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async function setupAdmin() {
|
||
|
|
try {
|
||
|
|
console.log('=== ASLE Admin Setup ===\n');
|
||
|
|
|
||
|
|
const email = await question('Admin email: ');
|
||
|
|
const password = await question('Admin password: ');
|
||
|
|
const role = await question('Role (admin/super_admin) [admin]: ') || 'admin';
|
||
|
|
|
||
|
|
// Check if admin already exists
|
||
|
|
const existing = await prisma.adminUser.findUnique({
|
||
|
|
where: { email },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (existing) {
|
||
|
|
console.log('\n❌ Admin user already exists!');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Hash password
|
||
|
|
const passwordHash = await bcrypt.hash(password, 10);
|
||
|
|
|
||
|
|
// Create admin user
|
||
|
|
const admin = await prisma.adminUser.create({
|
||
|
|
data: {
|
||
|
|
email,
|
||
|
|
passwordHash,
|
||
|
|
role: role as 'admin' | 'super_admin',
|
||
|
|
permissions: [],
|
||
|
|
active: true,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('\n✅ Admin user created successfully!');
|
||
|
|
console.log(` ID: ${admin.id}`);
|
||
|
|
console.log(` Email: ${admin.email}`);
|
||
|
|
console.log(` Role: ${admin.role}`);
|
||
|
|
} catch (error: any) {
|
||
|
|
console.error('\n❌ Error creating admin user:', error.message);
|
||
|
|
process.exit(1);
|
||
|
|
} finally {
|
||
|
|
rl.close();
|
||
|
|
await prisma.$disconnect();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
setupAdmin();
|
||
|
|
|