const Database = require('better-sqlite3'); const bcrypt = require('bcrypt'); const CryptoJS = require('crypto-js'); const path = require('path'); // Get encryption key from environment const FIELD_ENCRYPTION_KEY = process.env.FIELD_ENCRYPTION_KEY || 'dev_field_key_change_in_production_32chars_min!'; // Encryption function function encrypt(text) { if (!text) return null; try { return CryptoJS.AES.encrypt(text, FIELD_ENCRYPTION_KEY).toString(); } catch (error) { console.error('Encryption error:', error); return text; } } async function resetAdmin() { const dbPath = path.join(__dirname, 'skillmate.dev.db'); console.log(`Opening database at: ${dbPath}`); const db = new Database(dbPath); try { console.log('\n=== Resetting Admin User ===\n'); // Check if admin exists const existingAdmin = db.prepare('SELECT * FROM users WHERE username = ?').get('admin'); const password = 'admin123'; const hashedPassword = await bcrypt.hash(password, 10); const encryptedEmail = encrypt('admin@skillmate.local'); if (existingAdmin) { console.log('Admin user exists, updating...'); db.prepare(` UPDATE users SET password = ?, email = ?, role = 'admin', is_active = 1, updated_at = ? WHERE username = 'admin' `).run(hashedPassword, encryptedEmail, new Date().toISOString()); } else { console.log('Creating new admin user...'); const adminId = 'admin-' + Date.now(); db.prepare(` INSERT INTO users (id, username, email, password, role, is_active, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) `).run( adminId, 'admin', encryptedEmail, hashedPassword, 'admin', 1, new Date().toISOString(), new Date().toISOString() ); } console.log('\n✓ Admin user reset successfully!'); console.log('Username: admin'); console.log('Password: admin123'); console.log('Email: admin@skillmate.local'); } catch (error) { console.error('Error resetting admin:', error); process.exit(1); } finally { db.close(); } } // Run the reset resetAdmin();