@gingkoo/base-server 0.0.2-alpha.6 → 0.0.2-alpha.7
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.
|
@@ -17,11 +17,9 @@ async function authCheck(req, res, next) {
|
|
|
17
17
|
let { userid, exp } = data;
|
|
18
18
|
if (userid) {
|
|
19
19
|
req.headers['x-userid'] = userid;
|
|
20
|
-
let expires = new Date();
|
|
21
|
-
expires.setDate(expires.getDate() + 3);
|
|
22
20
|
if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
|
|
23
21
|
let token = jwt.generateToken(tokenData);
|
|
24
|
-
res
|
|
22
|
+
setTokenCookies(res, token);
|
|
25
23
|
}
|
|
26
24
|
}
|
|
27
25
|
return next();
|
|
@@ -55,11 +53,9 @@ function checkLogin(req, res, next) {
|
|
|
55
53
|
let { userid, exp } = data;
|
|
56
54
|
if (userid) {
|
|
57
55
|
req.headers['x-userid'] = userid;
|
|
58
|
-
let expires = new Date();
|
|
59
|
-
expires.setDate(expires.getDate() + 3);
|
|
60
56
|
if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
|
|
61
57
|
let token = jwt.generateToken(tokenData);
|
|
62
|
-
res
|
|
58
|
+
setTokenCookies(res, token);
|
|
63
59
|
}
|
|
64
60
|
}
|
|
65
61
|
return next();
|
|
@@ -101,9 +97,21 @@ function userAppAuthCheck(req, res, next) {
|
|
|
101
97
|
return next();
|
|
102
98
|
}
|
|
103
99
|
|
|
100
|
+
function setTokenCookies(res, token) {
|
|
101
|
+
let expires = new Date();
|
|
102
|
+
expires.setDate(expires.getDate() + config.jwt.exp);
|
|
103
|
+
|
|
104
|
+
if (config.jwt.domain) {
|
|
105
|
+
return res.cookie('X-Token', token, { expires, domain: config.jwt.domain });
|
|
106
|
+
} else {
|
|
107
|
+
return res.cookie('X-Token', token, { expires });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
104
111
|
module.exports = {
|
|
105
112
|
authCheck,
|
|
106
113
|
checkLogin,
|
|
107
114
|
appAuthCheck,
|
|
108
115
|
userAppAuthCheck,
|
|
116
|
+
setTokenCookies,
|
|
109
117
|
};
|
|
@@ -4,6 +4,7 @@ const path = require('path');
|
|
|
4
4
|
const express = require('express');
|
|
5
5
|
const config = require('../../../config/index');
|
|
6
6
|
|
|
7
|
+
const { setTokenCookies } = require('../../middleware/auth');
|
|
7
8
|
const authServices = require('../services/auth');
|
|
8
9
|
const ginfoServices = require('../../ginfo/index');
|
|
9
10
|
const userService = require('../../services/user');
|
|
@@ -185,7 +186,7 @@ router.get('/oauth/qywx/callback', async function (req, res) {
|
|
|
185
186
|
let expires = new Date();
|
|
186
187
|
expires.setDate(expires.getDate() + 3);
|
|
187
188
|
|
|
188
|
-
res
|
|
189
|
+
setTokenCookies(res, token).redirect(301, redirect_uri);
|
|
189
190
|
});
|
|
190
191
|
|
|
191
192
|
router.get('/gettoken', async function (req, res) {
|
package/backend/config/index.js
CHANGED
package/backend/routers/app.js
CHANGED
|
@@ -4,7 +4,7 @@ const nodemailer = require('nodemailer');
|
|
|
4
4
|
const bcrypt = require('bcryptjs');
|
|
5
5
|
const config = require('../config');
|
|
6
6
|
const jwt = require('../utils/jwt');
|
|
7
|
-
const { checkLogin } = require('../common/middleware/auth');
|
|
7
|
+
const { checkLogin, setTokenCookies } = require('../common/middleware/auth');
|
|
8
8
|
const { join, readFileContent } = require('../utils/path');
|
|
9
9
|
const userService = require('../common/services/user');
|
|
10
10
|
const generalConfig = require('../common/services/generalConfig');
|
|
@@ -43,11 +43,7 @@ router.post('/login', async (req, res) => {
|
|
|
43
43
|
userid: userinfo?.USER_ID,
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
expires.setDate(expires.getDate() + 3);
|
|
48
|
-
|
|
49
|
-
res.cookie('X-Token', token, { expires });
|
|
50
|
-
|
|
46
|
+
setTokenCookies(res, token);
|
|
51
47
|
res.sendOk({
|
|
52
48
|
token,
|
|
53
49
|
});
|
package/backend/utils/jwt.js
CHANGED
|
@@ -4,7 +4,7 @@ const config = require('../config');
|
|
|
4
4
|
// 生成token
|
|
5
5
|
function generateToken(data, forever = false, cert = config?.jwt?.auth_cert) {
|
|
6
6
|
let created = Math.floor(Date.now() / 1000);
|
|
7
|
-
let exp = created + config.jwt.exp;
|
|
7
|
+
let exp = created + 60 * 60 * 24 * config.jwt.exp;
|
|
8
8
|
if (forever) {
|
|
9
9
|
exp = created + 60 * 60 * 24 * 365 * 10; // 10 年
|
|
10
10
|
}
|