mdkcontroller 1.4.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/demoApp.js +94 -3
- package/main.js +4 -3
- package/package.json +1 -1
package/demoApp.js
CHANGED
|
@@ -11,8 +11,9 @@ const cfg = {
|
|
|
11
11
|
// ,trySkipAuth: function (req, res, next) {
|
|
12
12
|
// if (true) {
|
|
13
13
|
// next();
|
|
14
|
+
// return true;
|
|
14
15
|
// }
|
|
15
|
-
// return
|
|
16
|
+
// return false; to required login
|
|
16
17
|
// }
|
|
17
18
|
}
|
|
18
19
|
//global.allowRegister = true;
|
|
@@ -21,6 +22,7 @@ const _sv = await Root("khanhnbd", cfg);
|
|
|
21
22
|
// global.app = app;
|
|
22
23
|
// global.auth = auth;
|
|
23
24
|
// global.generateRandomString
|
|
25
|
+
// globbal.webAppPath = __parentAppPath;
|
|
24
26
|
global.db.data.yourTableName = global.db.data.yourTableName ?? [];
|
|
25
27
|
|
|
26
28
|
const newRouterApi = Router();
|
|
@@ -32,7 +34,7 @@ newRouterApi.get('/demoPhase2/CallNonAuth', async (req, res) => {
|
|
|
32
34
|
|
|
33
35
|
res.redirect('/login');
|
|
34
36
|
});
|
|
35
|
-
app.use("/api", newRouterApi);
|
|
37
|
+
global.app.use("/api", newRouterApi);
|
|
36
38
|
|
|
37
39
|
// const io = new socketIO.Server(_sv.serverBase);
|
|
38
40
|
// io.on("connection", (socket) => {
|
|
@@ -50,4 +52,93 @@ app.use("/api", newRouterApi);
|
|
|
50
52
|
// });
|
|
51
53
|
// });
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
import readline from 'readline';
|
|
56
|
+
|
|
57
|
+
const rl = readline.createInterface({
|
|
58
|
+
input: process.stdin,
|
|
59
|
+
output: process.stdout
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
function ask(question) {
|
|
63
|
+
return new Promise(resolve => {
|
|
64
|
+
rl.question(question, answer => resolve(answer));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const PORT = process.env.PORT || 8888;
|
|
68
|
+
_sv.startListen(PORT).then(() => {
|
|
69
|
+
console.log("Server started successfully, listening on port: " + PORT);
|
|
70
|
+
(async () => {
|
|
71
|
+
let keepGoing = true;
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
while (keepGoing) {
|
|
75
|
+
const input = await ask('Nhập gì đó (hoặc gõ "exit" để thoát): ');
|
|
76
|
+
|
|
77
|
+
if (input === 'exit') {
|
|
78
|
+
keepGoing = false;
|
|
79
|
+
} else {
|
|
80
|
+
if (input == "help") {
|
|
81
|
+
console.log(`Các lệnh hỗ trợ:
|
|
82
|
+
help: hiển thị danh sách lệnh
|
|
83
|
+
exit: thoát chờ lệnh
|
|
84
|
+
ua: thêm người dùng mới
|
|
85
|
+
ud: xóa người dùng
|
|
86
|
+
ul: danh sách người dùng`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (input.startsWith("ua")) {
|
|
90
|
+
const parts = input.split(" ");
|
|
91
|
+
if (parts.length != 3) {
|
|
92
|
+
console.log("Cú pháp: ua <username> <password>");
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const username = parts[1];
|
|
96
|
+
const password = parts[2];
|
|
97
|
+
const existingUser = userTable.find(user => user.username === username);
|
|
98
|
+
if (existingUser) {
|
|
99
|
+
existingUser.password = global.auth.encrypt(password);
|
|
100
|
+
existingUser.signcode = global.generateRandomString(100);
|
|
101
|
+
db.write();
|
|
102
|
+
console.log(`Người dùng ${username} đã được cập nhật.`);
|
|
103
|
+
} else {
|
|
104
|
+
const newUser = {
|
|
105
|
+
id: Sequense('users'),
|
|
106
|
+
username: username.toLowerCase(),
|
|
107
|
+
email: username + "@patsoft.com.vn",
|
|
108
|
+
password: global.auth.encrypt(password),
|
|
109
|
+
active: true,
|
|
110
|
+
signcode: global.generateRandomString(100),
|
|
111
|
+
}
|
|
112
|
+
userTable.push(newUser);
|
|
113
|
+
db.write();
|
|
114
|
+
console.log(`Người dùng ${username} đã được thêm.`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (input.startsWith("ud")) {
|
|
118
|
+
const parts = input.split(" ");
|
|
119
|
+
if (parts.length != 2) {
|
|
120
|
+
console.log("Cú pháp: ud <username>");
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const username = parts[1];
|
|
124
|
+
const userIndex = userTable.findIndex(user => user.username === username);
|
|
125
|
+
if (userIndex !== -1) {
|
|
126
|
+
userTable.splice(userIndex, 1);
|
|
127
|
+
db.write();
|
|
128
|
+
console.log(`Người dùng ${username} đã được xóa.`);
|
|
129
|
+
} else {
|
|
130
|
+
console.log(`Người dùng ${username} không tồn tại.`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (input.startsWith("ul")) {
|
|
134
|
+
console.log("Danh sách người dùng:");
|
|
135
|
+
userTable.forEach(user => {
|
|
136
|
+
console.log(`- ${user.username}`);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
rl.close();
|
|
143
|
+
})();
|
|
144
|
+
});
|
package/main.js
CHANGED
|
@@ -26,6 +26,8 @@ export default async function (appname, cfgHandler = {}) {
|
|
|
26
26
|
global.db = db;
|
|
27
27
|
global.app = app;
|
|
28
28
|
global.auth = auth;
|
|
29
|
+
globbal.webAppPath = __parentAppPath;
|
|
30
|
+
|
|
29
31
|
global.generateRandomString = (length = 8) => {
|
|
30
32
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
31
33
|
let result = '';
|
|
@@ -62,9 +64,8 @@ export default async function (appname, cfgHandler = {}) {
|
|
|
62
64
|
break;
|
|
63
65
|
default:
|
|
64
66
|
if (req.path.toLowerCase().indexOf("/pages/") >= 0) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
} else {
|
|
67
|
+
const allowSkip = cfgHandler.trySkipAuth && cfgHandler.trySkipAuth(req, res, next);
|
|
68
|
+
if (!allowSkip) {
|
|
68
69
|
res.redirect("/Cores/Login");
|
|
69
70
|
}
|
|
70
71
|
return;
|