mirror of
https://github.com/Alvin-Zilverstand/challenge-11.git
synced 2026-03-06 21:29:13 +01:00
Add create-admin script to package.json and set up /api/users route in server.js for user management functionality.
This commit is contained in:
42
scripts/createAdmin.js
Normal file
42
scripts/createAdmin.js
Normal file
@@ -0,0 +1,42 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user