-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (38 loc) · 1.04 KB
/
script.js
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
const User = require("./models/user.js");
const mongoose = require("mongoose");
const bcrypt = require("bcryptjs")
const dotenv = require("dotenv")
dotenv.config()
if (mongoose.connections[0].readyState) {
pass
}else{
// Use new db connection
mongoose.connect(process.env.DB_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
});
}
const adminName = "admin";
const adminEmail = "aryaraj132@gmail.com";
const adminPassword = "admin123";
var createAdmin = async function() {
const getUser = await User.findOne({email: adminEmail});
if(getUser) {
console.log("Admin already exists");
mongoose.connection.close()
return false;
}
const salt = await bcrypt.genSalt(10);
const hashedPass = await bcrypt.hash(adminPassword, salt)
const admin = await new User({
name:adminName,
email:adminEmail,
password:hashedPass,
isAdmin:true
})
await admin.save()
mongoose.connection.close()
console.log("Admin created successfully", admin);
return
};
createAdmin();