48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
const bcrypt = require('bcrypt');
|
||
const { User } = require('./models');
|
||
|
||
async function createTestUser() {
|
||
try {
|
||
console.log('连接数据库...');
|
||
|
||
// 手动创建用户,直接设置加密密码
|
||
const hashedPassword = await bcrypt.hash('123456', 10);
|
||
console.log('生成的密码哈希:', hashedPassword);
|
||
|
||
// 删除现有的testuser3(如果存在)
|
||
await User.destroy({
|
||
where: { username: 'testuser3' }
|
||
});
|
||
|
||
// 直接插入数据库,绕过模型钩子
|
||
const user = await User.create({
|
||
username: 'testuser3',
|
||
email: 'test3@example.com',
|
||
password: hashedPassword,
|
||
status: 'active'
|
||
}, {
|
||
hooks: false // 禁用钩子
|
||
});
|
||
|
||
console.log('用户创建成功:', {
|
||
id: user.id,
|
||
username: user.username,
|
||
email: user.email
|
||
});
|
||
|
||
// 验证密码
|
||
const isValid = await bcrypt.compare('123456', user.password);
|
||
console.log('密码验证结果:', isValid);
|
||
|
||
// 使用模型方法验证
|
||
const isValidModel = await user.validPassword('123456');
|
||
console.log('模型方法验证结果:', isValidModel);
|
||
|
||
} catch (error) {
|
||
console.error('错误:', error);
|
||
} finally {
|
||
process.exit(0);
|
||
}
|
||
}
|
||
|
||
createTestUser(); |