文件
jw-beauty/server/scripts/init-db.js
T
Guoguo 4b6e577dfb fix: correct init-db hashPassword call for bcrypt migration
hashPassword now uses bcrypt (single arg), not SHA-256 (password+salt).
The old call silently ignored the salt param but was misleading.
2026-04-28 19:06:11 -07:00

41 行
1.3 KiB
JavaScript

require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') })
const fs = require('fs')
const path = require('path')
const mysql = require('mysql2/promise')
const config = require('../src/config')
const { hashPassword, randomHex } = require('../src/lib/auth')
async function main() {
const conn = await mysql.createConnection({
host: config.db.host,
port: config.db.port,
user: config.db.user,
password: config.db.password,
database: config.db.database,
multipleStatements: true
})
const schema = fs.readFileSync(path.join(__dirname, '..', 'sql', 'schema.sql'), 'utf8')
await conn.query(schema)
await conn.query("ALTER TABLE devices MODIFY product_id VARCHAR(64) NOT NULL DEFAULT 'HOX_LIGHT_MASK'").catch(() => {})
const [rows] = await conn.execute('SELECT admin_id FROM admin_accounts WHERE username = ?', [config.admin.username])
if (rows.length === 0) {
const passwordHash = hashPassword(config.admin.password)
await conn.execute(
'INSERT INTO admin_accounts (username, password_hash, password_salt, real_name, role, status) VALUES (?, ?, ?, ?, ?, 1)',
[config.admin.username, passwordHash, '', '管理员', 'admin']
)
}
await conn.end()
console.log('Database initialized:', config.db.database)
}
main().catch(err => {
console.error(err)
process.exit(1)
})