const Database = require('better-sqlite3'); const bcryptjs = require('bcryptjs'); // Use bcryptjs instead of bcrypt const crypto = require('crypto'); const path = require('path'); // Hash function for email function hashEmail(email) { if (!email) return null; return crypto.createHash('sha256').update(email.toLowerCase()).digest('hex'); } async function fixAdminPassword() { const dbPath = path.join(__dirname, 'skillmate.dev.db'); console.log(`Opening database at: ${dbPath}`); const db = new Database(dbPath); try { console.log('\n=== Fixing Admin User ===\n'); const password = 'admin123'; const hashedPassword = await bcryptjs.hash(password, 10); const emailHash = hashEmail('admin@skillmate.local'); console.log('Setting admin password with bcryptjs...'); console.log('Email hash:', emailHash); // Update admin user const result = db.prepare(` UPDATE users SET password = ?, email_hash = ? WHERE username = 'admin' `).run(hashedPassword, emailHash); console.log('Rows updated:', result.changes); // Verify the update const adminUser = db.prepare('SELECT * FROM users WHERE username = ?').get('admin'); if (adminUser) { console.log('\nAdmin user updated:'); console.log('- Username:', adminUser.username); console.log('- Email hash:', adminUser.email_hash); console.log('- Password hash:', adminUser.password.substring(0, 20) + '...'); // Test password const isValid = await bcryptjs.compare('admin123', adminUser.password); console.log('- Password verification:', isValid ? 'PASS' : 'FAIL'); } console.log('\n✓ Admin user fixed successfully!'); console.log('Username: admin'); console.log('Password: admin123'); } catch (error) { console.error('Error fixing admin:', error); process.exit(1); } finally { db.close(); } } // Run the fix fixAdminPassword();