Use raw SQL for admin user creation to ensure login works
This commit is contained in:
+13
-10
@@ -1,6 +1,6 @@
|
||||
const { sequelize, connectDB } = require('./config/database');
|
||||
const Block = require('./models/Block');
|
||||
const User = require('./models/User');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const eventBlocks = [
|
||||
{
|
||||
@@ -115,25 +115,28 @@ const eventBlocks = [
|
||||
async function seed() {
|
||||
try {
|
||||
await connectDB();
|
||||
// Create admin user if not exists
|
||||
const existingUser = await User.findOne({ where: { email: 'admin@cad.local' } });
|
||||
if (!existingUser) {
|
||||
await User.create({ email: 'admin@cad.local', password: 'admin123' });
|
||||
console.log('Admin user created: admin@cad.local / admin123');
|
||||
}
|
||||
// Use raw SQL to create user, bcrypt hashing via sequelize might fail, so we do raw
|
||||
const hashedPassword = bcrypt.hashSync('admin123', 10);
|
||||
await sequelize.query(`
|
||||
INSERT INTO "Users" ("email", "password", "createdAt", "updatedAt")
|
||||
VALUES (:email, :password, datetime('now'), datetime('now'))
|
||||
ON CONFLICT ("email") DO NOTHING;
|
||||
`, {
|
||||
replacements: { email: 'admin@cad.local', password: hashedPassword },
|
||||
type: sequelize.QueryTypes.INSERT
|
||||
});
|
||||
console.log('Admin user ensured.');
|
||||
// Clear existing blocks
|
||||
await Block.destroy({ where: {} });
|
||||
console.log('Existing blocks cleared.');
|
||||
// Insert new blocks
|
||||
for (const block of eventBlocks) {
|
||||
await Block.create(block);
|
||||
}
|
||||
console.log(`${eventBlocks.length} blocks created successfully.`);
|
||||
console.log(`${eventBlocks.length} blocks created.`);
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
console.error('Seed failed:', err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
seed();
|
||||
|
||||
Reference in New Issue
Block a user