- Implemented a script to add an 'address' column to the 'bookings' table if it doesn't exist. - Created a script to add 'include_service', 'include_cleanup', and 'total_price' columns to the 'bookings' table. - Set up a 'users' table with an admin user and password hashing using bcrypt. - Developed login and logout API routes with JWT authentication. - Created a login page with form handling and error display. - Designed a profile page for Christian Wärme showcasing skills and experience. - Added a logout button component for admin interface. - Implemented tests for login functionality and visual calendar layout.
59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
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) |