-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-admin-user.mjs
More file actions
90 lines (75 loc) · 2.64 KB
/
Copy pathcreate-admin-user.mjs
File metadata and controls
90 lines (75 loc) · 2.64 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
import { PrismaClient } from '@prisma/client';
import argon from 'argon2';
import { config } from 'dotenv';
// Load environment variables
config();
const prisma = new PrismaClient();
async function createAdminUser() {
try {
console.log('🔨 Creating admin user "bolton"...');
// Check if user already exists
const existingUser = await prisma.user.findUnique({
where: { username: 'bolton' },
});
if (existingUser) {
console.log('⚠️ User "bolton" already exists. Updating to admin role...');
// Update existing user to admin role
const updatedUser = await prisma.user.update({
where: { username: 'bolton' },
data: {
role: ['ADMIN'],
},
});
console.log('✅ Successfully updated user "bolton" to admin role');
console.log(` User ID: ${updatedUser.id}`);
console.log(` Username: ${updatedUser.username}`);
console.log(` Role: ${updatedUser.role.join(', ')}`);
return;
}
// Generate M2FA codes
const md2faCodes = [];
for (let i = 0; i < 10; i++) {
const code = 'bigfat' + generateRandomString(20);
md2faCodes.push(code);
}
md2faCodes.sort();
// Hash the M2FA codes
const hashedMd2faCodes = await Promise.all(md2faCodes.map(code => argon.hash(code)));
// Create the admin user
const adminUser = await prisma.user.create({
data: {
username: 'bolton',
password: await argon.hash('admin123'), // Default password
role: ['ADMIN'],
md2faCodes: hashedMd2faCodes,
balance: 1000, // Give admin some initial balance
},
});
console.log('✅ Successfully created admin user "bolton"');
console.log(` User ID: ${adminUser.id}`);
console.log(` Username: ${adminUser.username}`);
console.log(` Role: ${adminUser.role.join(', ')}`);
console.log(` Default Password: admin123`);
console.log(` Initial Balance: $${adminUser.balance}`);
console.log('\n📋 M2FA Backup Codes (save these securely):');
md2faCodes.forEach((code, index) => {
console.log(` ${index + 1}: ${code}`);
});
} catch (error) {
console.error('❌ Error creating admin user:', error);
} finally {
await prisma.$disconnect();
}
}
// Function to generate a random string of given length
function generateRandomString(length) {
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let result = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
result += charset[randomIndex];
}
return result;
}
createAdminUser();