73 lines
2.0 KiB
JavaScript
73 lines
2.0 KiB
JavaScript
import bcrypt from 'bcryptjs';
|
||
import mysql from 'mysql2/promise';
|
||
import dotenv from 'dotenv';
|
||
|
||
dotenv.config();
|
||
|
||
async function seedAdmin() {
|
||
let connection;
|
||
|
||
try {
|
||
// Connect to database (use root for seeding)
|
||
connection = await mysql.createConnection({
|
||
host: process.env.DB_HOST || 'localhost',
|
||
user: 'root',
|
||
password: 'rootpassword',
|
||
database: process.env.DB_NAME || 'work_allocation',
|
||
port: process.env.DB_PORT || 3306
|
||
});
|
||
|
||
console.log('✅ Connected to database');
|
||
|
||
// Check if admin already exists
|
||
const [existingUsers] = await connection.query(
|
||
'SELECT id FROM users WHERE username = ?',
|
||
['admin']
|
||
);
|
||
|
||
if (existingUsers.length > 0) {
|
||
console.log('ℹ️ Admin user already exists, updating password...');
|
||
|
||
// Generate new password hash
|
||
const passwordHash = await bcrypt.hash('admin123', 10);
|
||
|
||
// Update existing admin user
|
||
await connection.query(
|
||
'UPDATE users SET password = ? WHERE username = ?',
|
||
[passwordHash, 'admin']
|
||
);
|
||
|
||
console.log('✅ Admin password updated successfully');
|
||
} else {
|
||
console.log('📝 Creating admin user...');
|
||
|
||
// Generate password hash
|
||
const passwordHash = await bcrypt.hash('admin123', 10);
|
||
|
||
// Insert admin user
|
||
await connection.query(
|
||
'INSERT INTO users (username, name, email, password, role) VALUES (?, ?, ?, ?, ?)',
|
||
['admin', 'Super Admin', 'admin@workallocate.com', passwordHash, 'SuperAdmin']
|
||
);
|
||
|
||
console.log('✅ Admin user created successfully');
|
||
}
|
||
|
||
console.log('');
|
||
console.log('🔑 Default Login Credentials:');
|
||
console.log(' Username: admin');
|
||
console.log(' Password: admin123');
|
||
console.log('');
|
||
|
||
} catch (error) {
|
||
console.error('❌ Error seeding admin user:', error.message);
|
||
process.exit(1);
|
||
} finally {
|
||
if (connection) {
|
||
await connection.end();
|
||
}
|
||
}
|
||
}
|
||
|
||
seedAdmin();
|