45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import mysql from 'mysql2/promise';
|
|
import bcrypt from 'bcryptjs';
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
async function updatePasswords() {
|
|
try {
|
|
const conn = await mysql.createConnection({
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: Number(process.env.DB_PORT) || 3306,
|
|
user: process.env.DB_USER || 'root',
|
|
password: process.env.DB_PASSWORD || '',
|
|
database: process.env.DB_NAME || 'employee_performance',
|
|
});
|
|
|
|
console.log('生成新的密码哈希...');
|
|
const hash = await bcrypt.hash('123456', 10);
|
|
console.log('新哈希:', hash);
|
|
|
|
// 测试旧哈希
|
|
const oldHash = '$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy';
|
|
const matchOld = await bcrypt.compare('123456', oldHash);
|
|
console.log('旧哈希验证:', matchOld);
|
|
|
|
// 更新所有用户密码
|
|
await conn.query('UPDATE user SET password = ?', [hash]);
|
|
console.log('✓ 所有用户密码已更新');
|
|
|
|
// 验证更新
|
|
const [rows] = await conn.query('SELECT username, password FROM user LIMIT 1');
|
|
const user = (rows as any)[0];
|
|
const matchNew = await bcrypt.compare('123456', user.password);
|
|
console.log('新密码验证:', matchNew);
|
|
|
|
await conn.end();
|
|
process.exit(0);
|
|
} catch (error: any) {
|
|
console.error('错误:', error.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
updatePasswords();
|