mdkcontroller 1.0.15 → 1.0.16
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/dk_modules/users.js +19 -20
- package/package.json +1 -1
package/dk_modules/users.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import authInstant from "./authorization.js";
|
|
2
2
|
import autoSequenceInstant from "./autoSequence.js";
|
|
3
|
-
import {
|
|
3
|
+
import {allowRegister, generateRandomToken, privateKey} from "./dkvar.js";
|
|
4
|
+
|
|
4
5
|
export default (router, db) => {
|
|
5
6
|
|
|
6
7
|
const auth = authInstant(db);
|
|
@@ -10,13 +11,13 @@ export default (router, db) => {
|
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
router.get("/users/gets", auth.validate, (req, res) => {
|
|
13
|
-
res.json({
|
|
14
|
+
res.json({message: "Get all users"});
|
|
14
15
|
});
|
|
15
16
|
|
|
16
17
|
router.get("/users/get", auth.validate, (req, res) => {
|
|
17
18
|
const userId = req.params.id;
|
|
18
|
-
const userO = tbUser.find({
|
|
19
|
-
res.json({
|
|
19
|
+
const userO = tbUser.find({id: parseInt(userId)});
|
|
20
|
+
res.json({message: `Get user with ID ${userId}`, user: userO});
|
|
20
21
|
});
|
|
21
22
|
router.get('/users/logout', async (req, res) => {
|
|
22
23
|
const accessToken = req.cookies?.access_token;
|
|
@@ -25,7 +26,7 @@ export default (router, db) => {
|
|
|
25
26
|
tbLoginToken.slice(indexToken, 1);
|
|
26
27
|
await db.write();
|
|
27
28
|
}
|
|
28
|
-
res.clearCookie('access_token', {
|
|
29
|
+
res.clearCookie('access_token', {httpOnly: true});
|
|
29
30
|
res.clearCookie('sessionUExt', {});
|
|
30
31
|
res.redirect('/login');
|
|
31
32
|
});
|
|
@@ -35,7 +36,8 @@ export default (router, db) => {
|
|
|
35
36
|
if (bodyParser.userName && bodyParser.password) {
|
|
36
37
|
let userLoging = tbUser.find(f => f.userName == bodyParser.userName.toLowerCase() && f.password == bodyParser.password);
|
|
37
38
|
if (userLoging) {
|
|
38
|
-
const
|
|
39
|
+
const userIdCode = await auth.encrypt(userLoging.userName);
|
|
40
|
+
const accessToken = userIdCode + generateRandomToken(100);
|
|
39
41
|
|
|
40
42
|
const expiredValue = new Date();
|
|
41
43
|
expiredValue.setMonth(expiredValue.getMonth() + 1);
|
|
@@ -47,14 +49,14 @@ export default (router, db) => {
|
|
|
47
49
|
expired: expiredValue
|
|
48
50
|
});
|
|
49
51
|
await db.write();
|
|
50
|
-
res.cookie('access_token', accessToken, {
|
|
51
|
-
res.cookie('sessionUExt',
|
|
52
|
-
res.json({
|
|
52
|
+
res.cookie('access_token', accessToken, {httpOnly: true, expires: expiredValue});
|
|
53
|
+
res.cookie('sessionUExt', userIdCode, {expires: expiredValue});
|
|
54
|
+
res.json({message: `Đăng nhập thành công.`, success: true});
|
|
53
55
|
} else {
|
|
54
|
-
res.json({
|
|
56
|
+
res.json({message: `Tài khoản hoặc mật khẩu không chính xác.`, success: false});
|
|
55
57
|
}
|
|
56
58
|
} else {
|
|
57
|
-
res.json({
|
|
59
|
+
res.json({message: `Không đủ thông tin để đăng nhập.`, success: false});
|
|
58
60
|
}
|
|
59
61
|
});
|
|
60
62
|
router.get("/users/tryGetAccess", (req, res) => {
|
|
@@ -63,13 +65,13 @@ export default (router, db) => {
|
|
|
63
65
|
const accessToken = req.cookies?.access_token;
|
|
64
66
|
const expiredValue = new Date();
|
|
65
67
|
expiredValue.setMonth(expiredValue.getMonth() + 1);
|
|
66
|
-
res.cookie('access_token', accessToken, {
|
|
68
|
+
res.cookie('access_token', accessToken, {httpOnly: true, expires: expiredValue});
|
|
67
69
|
}
|
|
68
|
-
res.json({
|
|
70
|
+
res.json({success: true, isLogin: isLogin});
|
|
69
71
|
});
|
|
70
72
|
router.post("/users/create", async (req, res) => {
|
|
71
73
|
if (!allowRegister) {
|
|
72
|
-
res.json({
|
|
74
|
+
res.json({message: `Chức năng đăng ký tài khoản mới không được cung cấp.`, success: false});
|
|
73
75
|
return;
|
|
74
76
|
}
|
|
75
77
|
const bodyParser = req.body;
|
|
@@ -78,7 +80,7 @@ export default (router, db) => {
|
|
|
78
80
|
if (!newUser) {
|
|
79
81
|
let validateUserName = tbUser.find(f => f.userName == bodyParser.userName.toLowerCase());
|
|
80
82
|
if (validateUserName) {
|
|
81
|
-
res.json({
|
|
83
|
+
res.json({message: `Tên tài khoản đã có người đăng ký.`, success: false});
|
|
82
84
|
return;
|
|
83
85
|
}
|
|
84
86
|
tbUser
|
|
@@ -105,19 +107,16 @@ export default (router, db) => {
|
|
|
105
107
|
});
|
|
106
108
|
}
|
|
107
109
|
} else {
|
|
108
|
-
res.json({
|
|
110
|
+
res.json({message: `Không đủ thông tin để tạo User mới.`, success: false});
|
|
109
111
|
}
|
|
110
112
|
});
|
|
111
113
|
|
|
112
114
|
console.log(
|
|
113
115
|
"Module dk_modules/users.js: (db)=>{ router: router, table: tbUser }"
|
|
114
116
|
);
|
|
115
|
-
return {
|
|
117
|
+
return {router: router, table: tbUser};
|
|
116
118
|
};
|
|
117
119
|
|
|
118
|
-
function generateToken(subText) {
|
|
119
|
-
return encodeMini(subText + generateRandomToken(100), privateKey);
|
|
120
|
-
}
|
|
121
120
|
// // Thêm một người dùng mới
|
|
122
121
|
// users
|
|
123
122
|
// .push({ id: 3, name: 'Bob Smith' })
|