import { createConnection } from 'mysql2/promise' async function createUsersTable() { const connection = await createConnection({ host: process.env.DB_HOST || 'localhost', user: process.env.DB_USER || 'root', password: process.env.DB_PASSWORD || '', database: process.env.DB_NAME || 'warme' }) try { // Create users table await connection.execute(` CREATE TABLE IF NOT EXISTS users ( id VARCHAR(36) PRIMARY KEY DEFAULT (UUID()), username VARCHAR(50) UNIQUE NOT NULL, password_hash VARCHAR(255) NOT NULL, salt VARCHAR(32) NOT NULL, role ENUM('admin') DEFAULT 'admin', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) `) console.log('✅ Users table created successfully') // Check if admin user exists const [rows] = await connection.execute( 'SELECT * FROM users WHERE username = ?', ['brotha'] ) if (rows.length === 0) { // Create admin user with salted password const bcrypt = await import('bcrypt') const crypto = await import('crypto') const salt = crypto.default.randomBytes(16).toString('hex') const saltedPassword = 'makefood1' + salt const passwordHash = await bcrypt.default.hash(saltedPassword, 12) await connection.execute( 'INSERT INTO users (username, password_hash, salt, role) VALUES (?, ?, ?, ?)', ['brotha', passwordHash, salt, 'admin'] ) console.log('✅ Admin user created with salted password') } else { console.log('ℹ️ Admin user already exists') } } catch (error) { console.error('❌ Error setting up users table:', error) } finally { await connection.end() } } createUsersTable().catch(console.error)