118 lines
3.4 KiB
JavaScript
118 lines
3.4 KiB
JavaScript
|
|
#!/usr/bin/env node
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Admin Password Change Script
|
|||
|
|
* Usage: node scripts/change-password.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 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 Şifre Değiştir ║');
|
|||
|
|
console.log('╚═══════════════════════════════════════════════════════════════╝\n');
|
|||
|
|
|
|||
|
|
// Connect to database
|
|||
|
|
await sequelize.authenticate();
|
|||
|
|
console.log('✓ Database bağlantısı başarılı\n');
|
|||
|
|
|
|||
|
|
// Get username
|
|||
|
|
const username = await question('Admin kullanıcı adı: ');
|
|||
|
|
|
|||
|
|
// Check if user exists
|
|||
|
|
const user = await AdminUser.findOne({ where: { username } });
|
|||
|
|
if (!user) {
|
|||
|
|
console.log('✗ Kullanıcı bulunamadı!');
|
|||
|
|
rl.close();
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Get new 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('Yeni şifre (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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Update password
|
|||
|
|
console.log('\n⏳ Şifre güncelleniyor...');
|
|||
|
|
|
|||
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|||
|
|
|
|||
|
|
await user.update({
|
|||
|
|
password_hash: hashedPassword,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
console.log('\n✓ Şifre başarıyla değiştirildi!');
|
|||
|
|
console.log(`\nKullanıcı: ${username}`);
|
|||
|
|
console.log('Yeni şifre: ********** (güvenli bir şekilde saklandı)');
|
|||
|
|
console.log('\n✓ Artık yeni şifrenizle giriş yapabilirsiniz.\n');
|
|||
|
|
|
|||
|
|
rl.close();
|
|||
|
|
process.exit(0);
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('\n✗ Hata:', error.message);
|
|||
|
|
rl.close();
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
})();
|
|||
|
|
|