mdkcontroller 1.4.8 → 1.4.10
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 +21 -4
- package/dk_modules/authorization.js +2 -0
- package/dk_modules/users.js +15 -8
- package/main.js +50 -1
- package/package.json +1 -1
package/demoApp.js
CHANGED
|
@@ -26,13 +26,30 @@ const _sv = await Root("khanhnbd", cfg);
|
|
|
26
26
|
global.db.data.yourTableName = global.db.data.yourTableName ?? [];
|
|
27
27
|
|
|
28
28
|
const newRouterApi = Router();
|
|
29
|
-
newRouterApi.get("/demoFunction/gets",
|
|
29
|
+
newRouterApi.get("/demoFunction/gets", global.auth.validate, (req, res) => {
|
|
30
30
|
res.json({ message: "Get all users" });
|
|
31
31
|
});
|
|
32
32
|
newRouterApi.get('/demoPhase2/CallNonAuth', async (req, res) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
if (req.user) {
|
|
34
|
+
const accessToken = req.user.accessTokenSQL;
|
|
35
|
+
if (accessToken) {
|
|
36
|
+
fetch("https://kkvalidatecenter.khanhnbd.io.vn/select?table=MovieItem", {
|
|
37
|
+
method: "GET",
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
'Authorization': `Bearer ${accessToken}`
|
|
41
|
+
},
|
|
42
|
+
}).then(response => {
|
|
43
|
+
if (response.ok) {
|
|
44
|
+
return response.json()
|
|
45
|
+
}
|
|
46
|
+
return { success: false, message: "Yêu cầu tới server không thành công." };
|
|
47
|
+
})
|
|
48
|
+
.then(data => {
|
|
49
|
+
res.json({ message: "Access granted", data: data });
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
36
53
|
});
|
|
37
54
|
global.app.use("/api", newRouterApi);
|
|
38
55
|
|
|
@@ -9,6 +9,7 @@ export default function () {
|
|
|
9
9
|
userId: user.id,
|
|
10
10
|
username: user.username,
|
|
11
11
|
signcode: user.signcode,
|
|
12
|
+
accessTokenSQL: user.accessTokenSQL || null
|
|
12
13
|
};
|
|
13
14
|
const token = jwt.sign(payload, secretKey);
|
|
14
15
|
return token;
|
|
@@ -49,6 +50,7 @@ export default function () {
|
|
|
49
50
|
return -1;
|
|
50
51
|
}
|
|
51
52
|
req.user = userO;
|
|
53
|
+
req.user.accessTokenSQL = jwtValidate.accessTokenSQL;
|
|
52
54
|
const expiredValue = new Date();
|
|
53
55
|
expiredValue.setMonth(expiredValue.getMonth() + 1);
|
|
54
56
|
res.cookie('access_token', accessToken, { httpOnly: true, expires: expiredValue });
|
package/dk_modules/users.js
CHANGED
|
@@ -56,17 +56,18 @@ export default (router, db) => {
|
|
|
56
56
|
router.post("/users/loginByCenter", async (req, res) => {
|
|
57
57
|
const bodyParser = req.body;
|
|
58
58
|
if (bodyParser.tokenWS) {
|
|
59
|
-
fetch(`https://kkvalidatecenter.khanhnbd.io.vn/
|
|
60
|
-
method: "
|
|
59
|
+
fetch(`https://kkvalidatecenter.khanhnbd.io.vn/loginByToken`, {
|
|
60
|
+
method: "POST",
|
|
61
61
|
headers: {
|
|
62
62
|
'Content-Type': 'application/json',
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
}).then(response => {
|
|
63
|
+
},
|
|
64
|
+
body: JSON.stringify({ sign: bodyParser.tokenWS, DatabaseName: global.appName })
|
|
65
|
+
}).then(async response => {
|
|
66
66
|
if (response.ok) {
|
|
67
67
|
return response.json()
|
|
68
68
|
}
|
|
69
|
-
|
|
69
|
+
const text = await response.text();
|
|
70
|
+
return { success: false, message: `${response.status}-${text}` };
|
|
70
71
|
})
|
|
71
72
|
.then(data => {
|
|
72
73
|
if (data.userName) {
|
|
@@ -85,8 +86,14 @@ export default (router, db) => {
|
|
|
85
86
|
tbUser.push(userLoging);
|
|
86
87
|
db.write();
|
|
87
88
|
}
|
|
89
|
+
const payloadX = {
|
|
90
|
+
id: userLoging.id,
|
|
91
|
+
username: userLoging.username,
|
|
92
|
+
signcode: userLoging.signcode,
|
|
93
|
+
accessTokenSQL: data.access_token
|
|
94
|
+
}
|
|
88
95
|
const userIdCode = auth.encrypt(userLoging.username);
|
|
89
|
-
const accessToken = auth.createJWT(
|
|
96
|
+
const accessToken = auth.createJWT(payloadX);
|
|
90
97
|
const expiredValue = new Date();
|
|
91
98
|
expiredValue.setMonth(expiredValue.getMonth() + 1);
|
|
92
99
|
res.cookie('access_token', accessToken, { httpOnly: true, expires: expiredValue });
|
|
@@ -98,7 +105,7 @@ export default (router, db) => {
|
|
|
98
105
|
res.json({ message: `Đăng nhập thành công.`, success: true });
|
|
99
106
|
}
|
|
100
107
|
} else {
|
|
101
|
-
res.json({ message: `Token không hợp lệ hoặc đã hết hạn.`, success: false })
|
|
108
|
+
res.json({ message: data.message || `Token không hợp lệ hoặc đã hết hạn.`, success: false })
|
|
102
109
|
}
|
|
103
110
|
});
|
|
104
111
|
} else {
|
package/main.js
CHANGED
|
@@ -23,11 +23,60 @@ export default async function (appname, cfgHandler = {}) {
|
|
|
23
23
|
const db = await dbInstant(appname + "-Database.json");
|
|
24
24
|
const auth = authInstant();
|
|
25
25
|
const userRoutes = userInstant(Router(), db);
|
|
26
|
+
global.appName = appname;
|
|
26
27
|
global.db = db;
|
|
27
28
|
global.app = app;
|
|
28
29
|
global.auth = auth;
|
|
29
30
|
global.webAppPath = __parentAppPath;
|
|
30
|
-
|
|
31
|
+
global.dksql = {
|
|
32
|
+
select: (accessToken, tableName, andQuery = "") => {
|
|
33
|
+
if (andQuery) {
|
|
34
|
+
andQuery = "&" + andQuery;
|
|
35
|
+
}
|
|
36
|
+
return fetch(`https://kkvalidatecenter.khanhnbd.io.vn/select?table=${tableName}${andQuery}`, {
|
|
37
|
+
method: "GET",
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
'Authorization': `Bearer ${accessToken}`
|
|
41
|
+
},
|
|
42
|
+
}).then(response => {
|
|
43
|
+
if (response.ok) {
|
|
44
|
+
return response.json();
|
|
45
|
+
}
|
|
46
|
+
return { success: false, message: "Yêu cầu tới server không thành công." };
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
update: (accessToken, data) => {
|
|
50
|
+
return fetch(`https://kkvalidatecenter.khanhnbd.io.vn/update`, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: {
|
|
53
|
+
'Content-Type': 'application/json',
|
|
54
|
+
'Authorization': `Bearer ${accessToken}`
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify(data)
|
|
57
|
+
}).then(response => {
|
|
58
|
+
if (response.ok) {
|
|
59
|
+
return response.json()
|
|
60
|
+
}
|
|
61
|
+
return { success: false, message: "Yêu cầu tới server không thành công." };
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
delete: (accessToken, data) => {
|
|
65
|
+
return fetch(`https://kkvalidatecenter.khanhnbd.io.vn/delete`, {
|
|
66
|
+
method: "DELETE",
|
|
67
|
+
headers: {
|
|
68
|
+
'Content-Type': 'application/json',
|
|
69
|
+
'Authorization': `Bearer ${accessToken}`
|
|
70
|
+
},
|
|
71
|
+
body: JSON.stringify(data)
|
|
72
|
+
}).then(response => {
|
|
73
|
+
if (response.ok) {
|
|
74
|
+
return response.json()
|
|
75
|
+
}
|
|
76
|
+
return { success: false, message: "Yêu cầu tới server không thành công." };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
31
80
|
global.generateRandomString = (length = 8) => {
|
|
32
81
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
33
82
|
let result = '';
|