feat: Add admin password management tools and debug logging
- Add setAdminPassword.js utility for setting password inside container - Add set-admin-password.sh for easy Docker exec password setting - Add reset-to-default-password.sh for quick testing (sets to admin123) - Add debug logging to password verification for troubleshooting - Improve error handling when password hash is missing These tools make it much easier to manage the admin password without dealing with bcrypt hash generation and .env file formatting issues. Usage: - Set custom password: ./scripts/set-admin-password.sh - Reset to default: ./scripts/reset-to-default-password.sh 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -26,7 +26,24 @@ class AuthService {
|
||||
|
||||
async verifyPassword(password) {
|
||||
try {
|
||||
const isValid = await bcrypt.compare(password, config.auth.adminPasswordHash);
|
||||
const hash = config.auth.adminPasswordHash;
|
||||
|
||||
// Debug logging in development
|
||||
if (config.app.env === 'development') {
|
||||
logger.debug(`Password verification debug:`, {
|
||||
hashExists: !!hash,
|
||||
hashLength: hash ? hash.length : 0,
|
||||
hashPrefix: hash ? hash.substring(0, 7) : 'none',
|
||||
passwordLength: password.length
|
||||
});
|
||||
}
|
||||
|
||||
if (!hash) {
|
||||
logger.error('No admin password hash configured');
|
||||
return false;
|
||||
}
|
||||
|
||||
const isValid = await bcrypt.compare(password, hash);
|
||||
return isValid;
|
||||
} catch (error) {
|
||||
logger.error('Password verification error:', error);
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const bcrypt = require('bcrypt');
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const readline = require('readline');
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
async function updateEnvFile(hash) {
|
||||
const envPath = path.join(__dirname, '../../../.env');
|
||||
|
||||
try {
|
||||
// Read the current .env file
|
||||
let envContent = await fs.readFile(envPath, 'utf8');
|
||||
|
||||
// Replace or add the ADMIN_PASSWORD_HASH
|
||||
if (envContent.includes('ADMIN_PASSWORD_HASH=')) {
|
||||
envContent = envContent.replace(/ADMIN_PASSWORD_HASH=.*$/m, `ADMIN_PASSWORD_HASH=${hash}`);
|
||||
} else {
|
||||
envContent += `\nADMIN_PASSWORD_HASH=${hash}\n`;
|
||||
}
|
||||
|
||||
// Write back to .env
|
||||
await fs.writeFile(envPath, envContent);
|
||||
console.log('✅ Password hash updated in .env file');
|
||||
|
||||
} catch (error) {
|
||||
console.error('⚠️ Could not update .env file:', error.message);
|
||||
console.log('\nPlease manually update your .env file with:');
|
||||
console.log(`ADMIN_PASSWORD_HASH=${hash}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function setAdminPassword() {
|
||||
console.log('=================================');
|
||||
console.log('MinIO WebUI - Set Admin Password');
|
||||
console.log('=================================\n');
|
||||
|
||||
const password = await new Promise((resolve) => {
|
||||
rl.question('Enter new admin password (min 8 chars): ', (answer) => {
|
||||
resolve(answer);
|
||||
});
|
||||
});
|
||||
|
||||
if (!password || password.length < 8) {
|
||||
console.error('\n❌ Error: Password must be at least 8 characters long.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('\nGenerating password hash...');
|
||||
|
||||
try {
|
||||
const hash = await bcrypt.hash(password, 12);
|
||||
|
||||
console.log('\n=================================');
|
||||
console.log('Password Hash Generated:');
|
||||
console.log('=================================\n');
|
||||
console.log(`Hash: ${hash}\n`);
|
||||
|
||||
// Try to update the .env file
|
||||
await updateEnvFile(hash);
|
||||
|
||||
console.log('\n=================================');
|
||||
console.log('✅ Admin password has been set!');
|
||||
console.log('=================================');
|
||||
console.log('\nYou can now login with:');
|
||||
console.log('Username: admin');
|
||||
console.log('Password: [the password you just set]');
|
||||
console.log('\nNote: You may need to restart the backend for changes to take effect.');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error generating hash:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
rl.close();
|
||||
}
|
||||
|
||||
// Run the script
|
||||
setAdminPassword();
|
||||
Reference in New Issue
Block a user