first commit
Some checks failed
Build Images and Deploy / Update-PROD-Stack (push) Failing after 14s
Some checks failed
Build Images and Deploy / Update-PROD-Stack (push) Failing after 14s
This commit is contained in:
55
src/setup-admin.js
Normal file
55
src/setup-admin.js
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Admin Setup Script
|
||||
*
|
||||
* Run this to create the first admin user (or promote an existing one).
|
||||
* Usage: node src/setup-admin.js <username> <password>
|
||||
* node src/setup-admin.js <username> (promote existing user)
|
||||
*/
|
||||
|
||||
// Load env
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
try {
|
||||
const envFile = fs.readFileSync(path.join(__dirname, '..', '.env'), 'utf8');
|
||||
envFile.split('\n').forEach(line => {
|
||||
const match = line.match(/^([^#=]+)=(.*)$/);
|
||||
if (match) process.env[match[1].trim()] = match[2].trim();
|
||||
});
|
||||
} catch (e) {}
|
||||
|
||||
const db = require('./config/database');
|
||||
|
||||
async function main() {
|
||||
await db.ready;
|
||||
|
||||
const { Users } = require('./models');
|
||||
|
||||
const username = process.argv[2];
|
||||
const password = process.argv[3];
|
||||
|
||||
if (!username) {
|
||||
console.log('Usage:');
|
||||
console.log(' Create new admin: node src/setup-admin.js <username> <password>');
|
||||
console.log(' Promote existing: node src/setup-admin.js <username>');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const existing = Users.findByUsername(username);
|
||||
|
||||
if (existing) {
|
||||
Users.makeAdmin(existing.id);
|
||||
console.log(`✅ User "${username}" has been promoted to admin.`);
|
||||
} else if (password) {
|
||||
const userId = Users.create(username, password);
|
||||
Users.makeAdmin(userId);
|
||||
console.log(`✅ Admin user "${username}" created successfully (ID: ${userId}).`);
|
||||
} else {
|
||||
console.log(`❌ User "${username}" not found. Provide a password to create a new admin.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
db.forceSave();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
main().catch(err => { console.error(err); process.exit(1); });
|
||||
Reference in New Issue
Block a user