Reflex/backend/utils/createAdmin.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2025-05-26 12:05:51 +07:00
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const User = require('../models/User');
// Load env vars
dotenv.config();
// Connect to DB
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log('MongoDB Connected'))
.catch(err => {
console.error('Error connecting to MongoDB:', err.message);
process.exit(1);
});
const createAdminAccount = async () => {
try {
2025-05-26 12:11:18 +07:00
// Check if admin already exists - use a valid email format
const adminExists = await User.findOne({ email: 'admin@example.com' });
2025-05-26 12:05:51 +07:00
if (adminExists) {
console.log('Admin account already exists');
2025-05-26 12:11:18 +07:00
console.log('Email: admin@example.com');
2025-05-26 12:05:51 +07:00
console.log('Password: admin124');
process.exit(0);
}
2025-05-26 12:11:18 +07:00
// Create admin user with a valid email format
2025-05-26 12:05:51 +07:00
const admin = new User({
name: 'Администратор',
2025-05-26 12:11:18 +07:00
email: 'admin@example.com',
2025-05-26 12:05:51 +07:00
password: 'admin124',
dateOfBirth: new Date('1990-01-01'),
gender: 'other',
isActive: true,
isAdmin: true,
location: {
city: 'Admin',
country: 'System'
2025-05-26 12:17:22 +07:00
} });
2025-05-26 12:05:51 +07:00
2025-05-26 12:17:22 +07:00
await admin.save();
console.log('Admin account created successfully');
2025-05-26 12:11:18 +07:00
console.log('Email: admin@example.com');
2025-05-26 12:05:51 +07:00
console.log('Password: admin124');
process.exit(0);
} catch (error) {
console.error('Error creating admin account:', error);
process.exit(1);
}
};
createAdminAccount();