mdkcontroller 1.4.0 → 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 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 true;
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
- _sv.startListen(8905);
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
+ });
@@ -26,7 +26,7 @@ export default function () {
26
26
  const db = global.db;
27
27
  const tbUser = db.data.users;
28
28
 
29
- const accessToken = req.cookies?.access_token;
29
+ const accessToken = req.cookies?.access_token || req.header('Authorization')?.replace('Bearer ', '');
30
30
  if (accessToken) {
31
31
  const jwtValidate = hideverifyJWT(accessToken);
32
32
  if (!jwtValidate) {
@@ -35,6 +35,9 @@ export default (router, db) => {
35
35
  expiredValue.setMonth(expiredValue.getMonth() + 1);
36
36
  res.cookie('access_token', accessToken, { httpOnly: true, expires: expiredValue });
37
37
  res.cookie('sessionUExt', userIdCode, { expires: expiredValue });
38
+ if (bodyParser.attacktoh) {
39
+ res.header('access_token', accessToken);
40
+ }
38
41
  res.json({ message: `Đăng nhập thành công.`, success: true });
39
42
  } else {
40
43
  res.json({ message: `Tài khoản hoặc mật khẩu không chính xác.`, success: false });
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
- if (cfgHandler.trySkipAuth && cfgHandler.trySkipAuth(req, res, next)) {
66
- //something work.
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;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "node-cache": "^5.1.2"
9
9
  },
10
10
  "name": "mdkcontroller",
11
- "version": "1.4.0",
11
+ "version": "1.4.2",
12
12
  "keywords": [],
13
13
  "author": "KHANHNBD <khanh272421@gmail.com>",
14
14
  "license": "ISC",
File without changes