Initial commit: 宁夏智慧养殖监管平台
This commit is contained in:
48
backend/create-test-user.js
Normal file
48
backend/create-test-user.js
Normal file
@@ -0,0 +1,48 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user