From b2cb3cb1bb79405a48bf3cebc2e8bd7a8e434f35 Mon Sep 17 00:00:00 2001 From: salvacybersec Date: Tue, 11 Nov 2025 05:02:08 +0300 Subject: [PATCH] admin user --- backend/.dockerignore | 9 ++- backend/scripts/change-password.js | 94 ++++++++++++++++++++++++++ backend/scripts/create-admin.js | 102 +++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 3 deletions(-) create mode 100755 backend/scripts/change-password.js create mode 100755 backend/scripts/create-admin.js diff --git a/backend/.dockerignore b/backend/.dockerignore index 0f4bb98..51cfd54 100644 --- a/backend/.dockerignore +++ b/backend/.dockerignore @@ -33,9 +33,12 @@ logs/ coverage/ .nyc_output/ -# Migrations (already applied) -migrations/ -seeders/ +# Migrations and seeders (run in entrypoint, keep for reference) +# migrations/ +# seeders/ + +# Scripts should be included (needed for admin creation) +# scripts/ - DO NOT IGNORE! # Temporary tmp/ diff --git a/backend/scripts/change-password.js b/backend/scripts/change-password.js new file mode 100755 index 0000000..86b452b --- /dev/null +++ b/backend/scripts/change-password.js @@ -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(); + diff --git a/backend/scripts/create-admin.js b/backend/scripts/create-admin.js new file mode 100755 index 0000000..ab4c7df --- /dev/null +++ b/backend/scripts/create-admin.js @@ -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(); +