Files
balikci/scripts/create-admin.js
salvacybersec f86cda2978 Domain support
2025-11-10 20:01:41 +03:00

143 lines
4.2 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Admin User Creation Script
* Usage: node scripts/create-admin.js
*/
const bcrypt = require('bcrypt');
const readline = require('readline');
const path = require('path');
// Set correct path for database config
process.chdir(path.join(__dirname, '../backend'));
const { sequelize } = require('../backend/src/config/database');
const AdminUser = require('../backend/src/models/AdminUser');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const question = (query) => new Promise((resolve) => rl.question(query, resolve));
const questionHidden = (query) => {
return new Promise((resolve) => {
rl.question(query, (answer) => {
resolve(answer);
});
rl._writeToOutput = () => {}; // Hide input
});
};
const validatePassword = (password) => {
if (password.length < 8) {
return 'Şifre en az 8 karakter olmalıdır!';
}
if (!/[a-zA-Z]/.test(password)) {
return 'Şifre en az bir harf içermelidir!';
}
if (!/[0-9]/.test(password)) {
return 'Şifre en az bir rakam içermelidir!';
}
return null;
};
(async () => {
try {
console.log('\n╔═══════════════════════════════════════════════════════════════╗');
console.log('║ Oltalama Panel - Admin Kullanıcı Oluştur ║');
console.log('╚═══════════════════════════════════════════════════════════════╝\n');
// Connect to database
await sequelize.authenticate();
console.log('✓ Database bağlantısı başarılı\n');
// Get username
let username;
while (true) {
username = await question('Admin kullanıcı adı (en az 3 karakter): ');
if (username.length >= 3) {
// Check if username exists
const existing = await AdminUser.findOne({ where: { username } });
if (existing) {
console.log('✗ Bu kullanıcı adı zaten kullanılıyor!\n');
continue;
}
break;
} else {
console.log('✗ Kullanıcı adı en az 3 karakter olmalıdır!\n');
}
}
// Get password
let password;
while (true) {
// Disable echo for password input
const originalWrite = rl._writeToOutput;
rl._writeToOutput = function (stringToWrite) {
if (stringToWrite.charCodeAt(0) === 13) {
rl.output.write('\n');
} else {
rl.output.write('*');
}
};
password = await question('Admin şifresi (en az 8 karakter, harf ve rakam): ');
console.log(); // New line after hidden input
const validationError = validatePassword(password);
if (validationError) {
console.log(`${validationError}\n`);
continue;
}
const passwordConfirm = await question('Şifreyi tekrar girin: ');
console.log(); // New line after hidden input
// Restore echo
rl._writeToOutput = originalWrite;
if (password !== passwordConfirm) {
console.log('✗ Şifreler eşleşmiyor!\n');
continue;
}
break;
}
// Get optional details
const fullName = await question('Tam ad (opsiyonel, ENTER ile geç): ');
const email = await question('E-posta (opsiyonel, ENTER ile geç): ');
// Create admin user
console.log('\n⏳ Admin kullanıcısı oluşturuluyor...');
const hashedPassword = await bcrypt.hash(password, 10);
await AdminUser.create({
username,
password_hash: hashedPassword,
email: email || null,
full_name: fullName || 'Administrator',
});
console.log('\n✓ Admin kullanıcısı başarıyla oluşturuldu!');
console.log(`\nKullanıcı adı: ${username}`);
console.log('Şifre: ********** (güvenli bir şekilde saklandı)');
if (fullName) console.log(`Tam ad: ${fullName}`);
if (email) console.log(`E-posta: ${email}`);
console.log('\n✓ Artık panele giriş yapabilirsiniz.\n');
rl.close();
process.exit(0);
} catch (error) {
console.error('\n✗ Hata:', error.message);
rl.close();
process.exit(1);
}
})();