forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-user.js
126 lines (114 loc) · 3.52 KB
/
create-user.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const path = require('path');
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') });
const { registerUser } = require('~/server/services/AuthService');
const { askQuestion, silentExit } = require('./helpers');
const User = require('~/models/User');
const connect = require('./connect');
(async () => {
await connect();
/**
* Show the welcome / help menu
*/
console.purple('--------------------------');
console.purple('Create a new user account!');
console.purple('--------------------------');
// If we don't have enough arguments, show the help menu
if (process.argv.length < 5) {
console.orange('Usage: npm run create-user <email> <name> <username>');
console.orange('Note: if you do not pass in the arguments, you will be prompted for them.');
console.orange(
'If you really need to pass in the password, you can do so as the 4th argument (not recommended for security).',
);
console.purple('--------------------------');
}
/**
* Set up the variables we need and get the arguments if they were passed in
*/
let email = '';
let password = '';
let name = '';
let username = '';
// If we have the right number of arguments, lets use them
if (process.argv.length >= 4) {
email = process.argv[2];
name = process.argv[3];
if (process.argv.length >= 5) {
username = process.argv[4];
}
if (process.argv.length >= 6) {
console.red('Warning: password passed in as argument, this is not secure!');
password = process.argv[5];
}
}
/**
* If we don't have the right number of arguments, lets prompt the user for them
*/
if (!email) {
email = await askQuestion('Email:');
}
// Validate the email
if (!email.includes('@')) {
console.red('Error: Invalid email address!');
silentExit(1);
}
const defaultName = email.split('@')[0];
if (!name) {
name = await askQuestion('Name: (default is: ' + defaultName + ')');
if (!name) {
name = defaultName;
}
}
if (!username) {
username = await askQuestion('Username: (default is: ' + defaultName + ')');
if (!username) {
username = defaultName;
}
}
if (!password) {
password = await askQuestion('Password: (leave blank, to generate one)');
if (!password) {
// Make it a random password, length 18
password = Math.random().toString(36).slice(-18);
console.orange('Your password is: ' + password);
}
}
// Validate the user doesn't already exist
const userExists = await User.findOne({ $or: [{ email }, { username }] });
if (userExists) {
console.red('Error: A user with that email or username already exists!');
silentExit(1);
}
/**
* Now that we have all the variables we need, lets create the user
*/
const user = { email, password, name, username, confirm_password: password };
let result;
try {
result = await registerUser(user);
} catch (error) {
console.red('Error: ' + error.message);
silentExit(1);
}
// Check the result
if (result.status !== 200) {
console.red('Error: ' + result.message);
silentExit(1);
}
// Done!
const userCreated = await User.findOne({ $or: [{ email }, { username }] });
if (userCreated) {
console.green('User created successfully!');
silentExit(0);
}
})();
process.on('uncaughtException', (err) => {
if (!err.message.includes('fetch failed')) {
console.error('There was an uncaught error:');
console.error(err);
}
if (err.message.includes('fetch failed')) {
return;
} else {
process.exit(1);
}
});