Files
kokken/scripts/setup-users.mjs
Alex Polo cbc1e31440 feat: Add address column and booking extras to bookings table
- 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.
2025-11-07 10:58:19 +01:00

59 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)