This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
createSuperuser.js
103 lines (86 loc) · 2.54 KB
/
createSuperuser.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/node
'use strict';
const userDao = require("./dao/userDao");
const readline = require("readline");
const rl = readline.createInterface(
process.stdin, process.stdout);
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("\x1B[2K\x1B[200D"+ rl.query+ "*".repeat(rl.line.length));
else
rl.output.write(stringToWrite);
};
function ask(query) {
rl.stdoutMuted = false;
rl.query = query;
return new Promise((resolve) => {
rl.question(query, (input) => resolve(input) );
});
}
function askHidden(query){
rl.stdoutMuted = true;
rl.query = query;
return new Promise((resolve) => {
rl.question(query, (input) =>{
console.log("");
resolve(input)
});
});
}
async function readUsername(){
while(true){
let username = await ask("Username:");
if(username.length < 3){
console.log("Too short username. At least 3 characters required.");
}
else if(await userDao.isUsernameTaken(username)){
console.log("This username is already taken.");
}
else{
return username;
}
}
}
async function readPassword(){
while(true){
let pass1 = await askHidden("Password:");
let pass2 = await askHidden("Repeat password:");
if(pass1==pass2)return pass1;
console.log("Passwords do not match, please re-enter.");
}
}
async function readEmail(){
while(true){
let email = await ask("Email:");
if(!userDao.isEmailValid(email)){
console.log("This is not email, please re-enter.");
}
else if(await userDao.isEmailTaken(email)){
console.log("This email is already taken.");
}
else{
return email;
}
}
}
async function createSuperuser(){
let username = await readUsername();
let password = await readPassword();
let given_name = await ask("Given name:");
let family_name = await ask("Family name:");
let email = await readEmail();
let admin = true;
let result = await userDao.postUser({username, password, given_name, family_name, email, admin}, true);
if(!result.success){
throw result.message;
}
}
console.log("Creating an administrator account.")
createSuperuser().then(function(){
console.log("Successfully created superuser account.");
process.exit(0);
}).catch(function(err){
console.error("Exception occured");
console.error(err);
process.exit(1);
});