mirror of
https://github.com/Alvin-Zilverstand/challenge-11.git
synced 2026-03-06 02:56:27 +01:00
42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const bcrypt = require('bcryptjs');
|
|
const User = require('../models/User');
|
|
require('dotenv').config();
|
|
|
|
async function createAdminUser() {
|
|
try {
|
|
// Connect to MongoDB
|
|
await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/car-tuning-crm');
|
|
console.log('Connected to MongoDB');
|
|
|
|
// Check if admin user already exists
|
|
const existingAdmin = await User.findOne({ username: 'admin' });
|
|
if (existingAdmin) {
|
|
console.log('Admin user already exists');
|
|
process.exit(0);
|
|
}
|
|
|
|
// Create admin user
|
|
const salt = await bcrypt.genSalt(10);
|
|
const hashedPassword = await bcrypt.hash('admin123', salt);
|
|
|
|
const adminUser = new User({
|
|
username: 'admin',
|
|
password: hashedPassword,
|
|
role: 'admin'
|
|
});
|
|
|
|
await adminUser.save();
|
|
console.log('Admin user created successfully');
|
|
console.log('Username: admin');
|
|
console.log('Password: admin123');
|
|
|
|
} catch (error) {
|
|
console.error('Error creating admin user:', error);
|
|
} finally {
|
|
await mongoose.disconnect();
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
createAdminUser();
|