-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-admin.js
More file actions
54 lines (46 loc) · 1.63 KB
/
create-admin.js
File metadata and controls
54 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fs = require('fs');
const envText = fs.readFileSync('.env.local', 'utf8');
const env = {};
envText.split('\n').forEach(line => {
const match = line.match(/^([^#\s]+)=(.*)$/);
if (match) env[match[1]] = match[2];
});
Object.assign(process.env, env);
const { PrismaClient } = require('@prisma/client');
const crypto = require('crypto');
const prisma = new PrismaClient();
const salt = process.env.ADMIN_SESSION_SECRET;
const email = process.env.ADMIN_NOTIFY_EMAIL || 'admin@example.com';
const rawPassword = process.env.ADMIN_PASSWORD;
if (!salt || !rawPassword) {
console.error("Missing ADMIN_SESSION_SECRET or ADMIN_PASSWORD in .env.local");
process.exit(1);
}
const util = require('util');
const scryptAsync = util.promisify(crypto.scrypt);
async function main() {
const newSalt = crypto.randomBytes(16).toString('hex');
const scryptHashBuffer = await scryptAsync(rawPassword, newSalt, 64);
const passwordHash = `${newSalt}:${scryptHashBuffer.toString('hex')}`;
const existing = await prisma.adminUser.findUnique({ where: { email } });
if (existing) {
console.log(`Admin user ${email} already exists. Updating password...`);
await prisma.adminUser.update({
where: { email },
data: { passwordHash, role: 'SUPER_ADMIN', isActive: true }
});
console.log("Updated successfully!");
} else {
console.log(`Creating Admin user ${email}...`);
await prisma.adminUser.create({
data: {
email,
passwordHash,
role: 'SUPER_ADMIN',
isActive: true
}
});
console.log("Created successfully!");
}
}
main().catch(console.error).finally(() => prisma.$disconnect());