-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateAdmin.js
More file actions
66 lines (55 loc) · 1.86 KB
/
Copy pathcreateAdmin.js
File metadata and controls
66 lines (55 loc) · 1.86 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
import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
const createAdminUser = async () => {
try {
// Connect to MongoDB
await mongoose.connect(process.env.MONGODB_URI);
console.log('✅ Connected to MongoDB');
// Define User schema (simplified)
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
role: String,
createdAt: Date,
updatedAt: Date
});
const User = mongoose.model('User', userSchema);
// Check if admin already exists
const existingAdmin = await User.findOne({ email: 'admin@bookstore.com' });
if (existingAdmin) {
console.log('⚠️ Admin user already exists!');
console.log('Email:', existingAdmin.email);
console.log('Role:', existingAdmin.role);
// Update to admin if not already
if (existingAdmin.role !== 'admin') {
existingAdmin.role = 'admin';
await existingAdmin.save();
console.log('✅ Updated existing user to admin role');
}
} else {
// Create new admin user
const adminUser = await User.create({
name: 'Admin User',
email: 'admin@bookstore.com',
password: '$2a$10$rGylmi4HwZCOETJvyl0dFO9no3nhRhrenzd2JRoFFPcFpzt4bsh.2',
role: 'admin',
createdAt: new Date(),
updatedAt: new Date()
});
console.log('✅ Admin user created successfully!');
console.log('📧 Email:', adminUser.email);
console.log('🔑 Password: admin123');
console.log('👤 Role:', adminUser.role);
}
// Close connection
await mongoose.connection.close();
console.log('\n✅ Done! You can now login with:');
console.log(' Email: admin@bookstore.com');
console.log(' Password: admin123');
} catch (error) {
console.error('❌ Error:', error.message);
}
};
createAdminUser();