-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-admin.js
More file actions
66 lines (57 loc) · 1.88 KB
/
Copy pathcreate-admin.js
File metadata and controls
66 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
require('dotenv').config();
// User schema (simplified)
const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
name: { type: String, required: true },
userType: { type: String, required: true, enum: ['admin', 'event_office'] },
isVerified: { type: Boolean, default: true },
status: { type: String, enum: ['active', 'blocked'], default: 'active' },
createdAt: { type: Date, default: Date.now }
});
// Hash password before saving
userSchema.pre('save', async function(next) {
if (!this.isModified('password')) return next();
try {
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
next();
} catch (error) {
next(error);
}
});
const User = mongoose.model('User', userSchema);
async function createAdmin() {
try {
await mongoose.connect(process.env.MONGO_URI);
console.log('Connected to MongoDB');
// Check if admin already exists
const existingAdmin = await User.findOne({ email: 'admin@test.com' });
if (existingAdmin) {
console.log('Admin user already exists:', existingAdmin.email);
return;
}
// Create admin user
const adminUser = new User({
email: 'admin@test.com',
password: 'admin123',
name: 'Admin User',
userType: 'admin',
isVerified: true,
status: 'active'
});
await adminUser.save();
console.log('✅ Admin user created successfully:', adminUser.email);
console.log('You can now login with:');
console.log('Email: admin@test.com');
console.log('Password: admin123');
} catch (error) {
console.error('❌ Error creating admin user:', error);
} finally {
await mongoose.disconnect();
console.log('Disconnected from MongoDB');
}
}
createAdmin();