forked from Mahakisore7/Dev-Hack-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateAdmin.js
More file actions
43 lines (36 loc) · 1.23 KB
/
createAdmin.js
File metadata and controls
43 lines (36 loc) · 1.23 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
import mongoose from "mongoose";
import bcrypt from "bcryptjs";
import User from "./models/User.js";
import "dotenv/config";
const createAdmin = async () => {
try {
// Connect to DB
await mongoose.connect(process.env.MONGODB_URI);
console.log("✅ DB Connected");
// 1. Check if admin already exists
const existingAdmin = await User.findOne({ email: "admin@prometeo.com" });
if (existingAdmin) {
console.log("⚠️ Admin already exists. You can login now.");
process.exit();
}
// 2. Hash Password
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash("admin123", salt);
// 3. Create User
const newAdmin = new User({
username: "Super Admin",
email: "admin@prometeo.com",
password: hashedPassword,
role: "admin"
});
await newAdmin.save();
console.log("🎉 SUCCESS: Admin Created!");
console.log("📧 Email: admin@prometeo.com");
console.log("🔑 Pass: admin123");
process.exit();
} catch (error) {
console.error("❌ Error:", error);
process.exit(1);
}
};
createAdmin();