-
Notifications
You must be signed in to change notification settings - Fork 13
/
create-user-cli.js
53 lines (45 loc) · 1.22 KB
/
create-user-cli.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//this file is used to create admin user using CLI
const CryptoJS = require("crypto-js");
const readline = require("readline");
const connectToMongo = require("./utils/db");
const User = require("./models/User");
connectToMongo();
// Readline interface for taking user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Function to hash password
const hashPassword = (password) => {
const hashedPassword = CryptoJS.SHA256(password).toString();
return hashedPassword;
};
// Function to create a new user
const createUser = (name, email, password) => {
const hashedPassword = hashPassword(password);
const user = new User({
name: name,
email: email,
password: hashedPassword,
});
user
.save()
.then(() => {
console.log("User created successfully!");
rl.close();
process.exit(0);
})
.catch((err) => {
console.error(err);
rl.close();
process.exit(0);
});
};
// Prompt for name, email, and password
rl.question("Enter name: ", function (name) {
rl.question("Enter email: ", function (email) {
rl.question("Enter password: ", function (password) {
createUser(name, email, password);
});
});
});