diff --git a/backend/src/services/auth.service.js b/backend/src/services/auth.service.js index 07043e2..c3a8880 100644 --- a/backend/src/services/auth.service.js +++ b/backend/src/services/auth.service.js @@ -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); diff --git a/backend/src/utils/setAdminPassword.js b/backend/src/utils/setAdminPassword.js new file mode 100644 index 0000000..563802b --- /dev/null +++ b/backend/src/utils/setAdminPassword.js @@ -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(); \ No newline at end of file diff --git a/scripts/reset-to-default-password.sh b/scripts/reset-to-default-password.sh new file mode 100755 index 0000000..b85baec --- /dev/null +++ b/scripts/reset-to-default-password.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +set -e + +echo "======================================" +echo "MinIO WebUI - Reset to Default Password" +echo "======================================" +echo "" + +# Default password hash for "admin123" +DEFAULT_HASH='$2b$12$LQv7c3SijBJLhSHPDOLkDe7YfPzFaXhJNXLKqFsFC37GA9xc1wSNi' + +# Check if .env exists +if [ ! -f .env ]; then + echo "❌ Error: .env file not found" + exit 1 +fi + +# Update the .env file +if grep -q "ADMIN_PASSWORD_HASH=" .env; then + # Replace existing hash + if [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + sed -i '' "s/ADMIN_PASSWORD_HASH=.*/ADMIN_PASSWORD_HASH=$DEFAULT_HASH/" .env + else + # Linux + sed -i "s/ADMIN_PASSWORD_HASH=.*/ADMIN_PASSWORD_HASH=$DEFAULT_HASH/" .env + fi +else + # Add hash if not present + echo "ADMIN_PASSWORD_HASH=$DEFAULT_HASH" >> .env +fi + +echo "✅ Password reset to default!" +echo "" +echo "Default credentials:" +echo " Username: admin" +echo " Password: admin123" +echo "" + +# Restart backend if running +if docker ps | grep -q minio-webui-backend; then + if docker ps | grep -q minio-webui-backend-dev; then + CONTAINER="minio-webui-backend-dev" + else + CONTAINER="minio-webui-backend" + fi + + echo "Restarting backend container..." + docker restart $CONTAINER + echo "✅ Backend restarted." +fi + +echo "" +echo "You can now login with: admin / admin123" +echo "" \ No newline at end of file diff --git a/scripts/set-admin-password.sh b/scripts/set-admin-password.sh new file mode 100755 index 0000000..a378498 --- /dev/null +++ b/scripts/set-admin-password.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e + +echo "======================================" +echo "MinIO WebUI - Set Admin Password" +echo "======================================" +echo "" + +# Check if docker-compose is running +if ! docker ps | grep -q minio-webui-backend; then + echo "❌ Error: Backend container is not running." + echo "" + echo "Please start the containers first:" + echo " docker compose -f docker-compose.dev.yml up -d" + exit 1 +fi + +# Determine which container name to use +if docker ps | grep -q minio-webui-backend-dev; then + CONTAINER="minio-webui-backend-dev" +else + CONTAINER="minio-webui-backend" +fi + +echo "Using container: $CONTAINER" +echo "" + +# Run the password setter inside the container +docker exec -it $CONTAINER node src/utils/setAdminPassword.js + +echo "" +echo "Restarting backend container..." +docker restart $CONTAINER + +echo "" +echo "✅ Backend restarted. You can now login with your new password." +echo "" \ No newline at end of file