admin user
This commit is contained in:
@@ -33,9 +33,12 @@ logs/
|
|||||||
coverage/
|
coverage/
|
||||||
.nyc_output/
|
.nyc_output/
|
||||||
|
|
||||||
# Migrations (already applied)
|
# Migrations and seeders (run in entrypoint, keep for reference)
|
||||||
migrations/
|
# migrations/
|
||||||
seeders/
|
# seeders/
|
||||||
|
|
||||||
|
# Scripts should be included (needed for admin creation)
|
||||||
|
# scripts/ - DO NOT IGNORE!
|
||||||
|
|
||||||
# Temporary
|
# Temporary
|
||||||
tmp/
|
tmp/
|
||||||
|
|||||||
94
backend/scripts/change-password.js
Executable file
94
backend/scripts/change-password.js
Executable file
@@ -0,0 +1,94 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change Admin Password Script
|
||||||
|
* Usage: node scripts/change-password.js
|
||||||
|
*/
|
||||||
|
|
||||||
|
const readline = require('readline');
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
// Load environment
|
||||||
|
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
||||||
|
|
||||||
|
// Load database and models
|
||||||
|
const { sequelize } = require('../src/config/database');
|
||||||
|
const { AdminUser } = require('../src/models');
|
||||||
|
|
||||||
|
const rl = readline.createInterface({
|
||||||
|
input: process.stdin,
|
||||||
|
output: process.stdout
|
||||||
|
});
|
||||||
|
|
||||||
|
function question(query) {
|
||||||
|
return new Promise(resolve => rl.question(query, resolve));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function changePassword() {
|
||||||
|
try {
|
||||||
|
console.log('\n╔═══════════════════════════════════════════════════════════╗');
|
||||||
|
console.log('║ 🔐 Change Admin Password - Oltalama Panel ║');
|
||||||
|
console.log('╚═══════════════════════════════════════════════════════════╝\n');
|
||||||
|
|
||||||
|
// Test database connection
|
||||||
|
await sequelize.authenticate();
|
||||||
|
console.log('✅ Database connection successful\n');
|
||||||
|
|
||||||
|
// Get username
|
||||||
|
const username = await question('👤 Enter admin username: ');
|
||||||
|
if (!username) {
|
||||||
|
console.log('❌ Username is required');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if user exists
|
||||||
|
const user = await AdminUser.findOne({ where: { username } });
|
||||||
|
if (!user) {
|
||||||
|
console.log('❌ User not found');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get new password
|
||||||
|
let newPassword;
|
||||||
|
while (true) {
|
||||||
|
newPassword = await question('🔑 Enter new password (min 8 characters): ');
|
||||||
|
if (!newPassword || newPassword.length < 8) {
|
||||||
|
console.log('❌ Password must be at least 8 characters\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const confirmPassword = await question('🔑 Confirm new password: ');
|
||||||
|
if (newPassword !== confirmPassword) {
|
||||||
|
console.log('❌ Passwords do not match\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password
|
||||||
|
console.log('\n⏳ Updating password...');
|
||||||
|
const hashedPassword = await bcrypt.hash(newPassword, 10);
|
||||||
|
|
||||||
|
// Update password
|
||||||
|
await user.update({
|
||||||
|
password: hashedPassword,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('\n✅ Password updated successfully!');
|
||||||
|
console.log(`👤 Username: ${username}\n`);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('\n❌ Error changing password:', error.message);
|
||||||
|
process.exit(1);
|
||||||
|
} finally {
|
||||||
|
rl.close();
|
||||||
|
await sequelize.close();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run
|
||||||
|
changePassword();
|
||||||
|
|
||||||
102
backend/scripts/create-admin.js
Executable file
102
backend/scripts/create-admin.js
Executable file
@@ -0,0 +1,102 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create Admin User Script
|
||||||
|
* Usage: node scripts/create-admin.js
|
||||||
|
*/
|
||||||
|
|
||||||
|
const readline = require('readline');
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
// Load environment
|
||||||
|
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
||||||
|
|
||||||
|
// Load database and models
|
||||||
|
const { sequelize } = require('../src/config/database');
|
||||||
|
const { AdminUser } = require('../src/models');
|
||||||
|
|
||||||
|
const rl = readline.createInterface({
|
||||||
|
input: process.stdin,
|
||||||
|
output: process.stdout
|
||||||
|
});
|
||||||
|
|
||||||
|
function question(query) {
|
||||||
|
return new Promise(resolve => rl.question(query, resolve));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createAdmin() {
|
||||||
|
try {
|
||||||
|
console.log('\n╔═══════════════════════════════════════════════════════════╗');
|
||||||
|
console.log('║ 🔐 Create Admin User - Oltalama Panel ║');
|
||||||
|
console.log('╚═══════════════════════════════════════════════════════════╝\n');
|
||||||
|
|
||||||
|
// Test database connection
|
||||||
|
await sequelize.authenticate();
|
||||||
|
console.log('✅ Database connection successful\n');
|
||||||
|
|
||||||
|
// Get username
|
||||||
|
let username;
|
||||||
|
while (true) {
|
||||||
|
username = await question('👤 Enter admin username: ');
|
||||||
|
if (!username || username.length < 3) {
|
||||||
|
console.log('❌ Username must be at least 3 characters\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if username exists
|
||||||
|
const existingUser = await AdminUser.findOne({ where: { username } });
|
||||||
|
if (existingUser) {
|
||||||
|
console.log('❌ Username already exists\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get password
|
||||||
|
let password;
|
||||||
|
while (true) {
|
||||||
|
password = await question('🔑 Enter admin password (min 8 characters): ');
|
||||||
|
if (!password || password.length < 8) {
|
||||||
|
console.log('❌ Password must be at least 8 characters\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const confirmPassword = await question('🔑 Confirm password: ');
|
||||||
|
if (password !== confirmPassword) {
|
||||||
|
console.log('❌ Passwords do not match\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password
|
||||||
|
console.log('\n⏳ Creating admin user...');
|
||||||
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
|
// Create admin user
|
||||||
|
await AdminUser.create({
|
||||||
|
username,
|
||||||
|
password: hashedPassword,
|
||||||
|
is_admin: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('\n✅ Admin user created successfully!');
|
||||||
|
console.log(`👤 Username: ${username}`);
|
||||||
|
console.log('\n🎉 You can now login to the admin panel!\n');
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('\n❌ Error creating admin user:', error.message);
|
||||||
|
process.exit(1);
|
||||||
|
} finally {
|
||||||
|
rl.close();
|
||||||
|
await sequelize.close();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run
|
||||||
|
createAdmin();
|
||||||
|
|
||||||
Reference in New Issue
Block a user