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 { sequelize, connectDB } = require('./config/database');
|
||||||
const Block = require('./models/Block');
|
const Block = require('./models/Block');
|
||||||
const User = require('./models/User');
|
const bcrypt = require('bcryptjs');
|
||||||
|
|
||||||
const eventBlocks = [
|
const eventBlocks = [
|
||||||
{
|
{
|
||||||
@@ -115,25 +115,28 @@ const eventBlocks = [
|
|||||||
async function seed() {
|
async function seed() {
|
||||||
try {
|
try {
|
||||||
await connectDB();
|
await connectDB();
|
||||||
// Create admin user if not exists
|
// Use raw SQL to create user, bcrypt hashing via sequelize might fail, so we do raw
|
||||||
const existingUser = await User.findOne({ where: { email: 'admin@cad.local' } });
|
const hashedPassword = bcrypt.hashSync('admin123', 10);
|
||||||
if (!existingUser) {
|
await sequelize.query(`
|
||||||
await User.create({ email: 'admin@cad.local', password: 'admin123' });
|
INSERT INTO "Users" ("email", "password", "createdAt", "updatedAt")
|
||||||
console.log('Admin user created: admin@cad.local / admin123');
|
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
|
// Clear existing blocks
|
||||||
await Block.destroy({ where: {} });
|
await Block.destroy({ where: {} });
|
||||||
console.log('Existing blocks cleared.');
|
console.log('Existing blocks cleared.');
|
||||||
// Insert new blocks
|
|
||||||
for (const block of eventBlocks) {
|
for (const block of eventBlocks) {
|
||||||
await Block.create(block);
|
await Block.create(block);
|
||||||
}
|
}
|
||||||
console.log(`${eventBlocks.length} blocks created successfully.`);
|
console.log(`${eventBlocks.length} blocks created.`);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Seed failed:', err);
|
console.error('Seed failed:', err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
seed();
|
seed();
|
||||||
|
|||||||
Reference in New Issue
Block a user