@gingkoo/base-server 0.0.1-alpha.5 → 0.0.1-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.
@@ -7,16 +7,22 @@ const { loginType, getLoginMethod } = require('../services/login');
7
7
  async function authCheck(req, res, next) {
8
8
  if (req.login) return next();
9
9
  let token = req.cookies['X-Token'] || req.get('X-Token');
10
+ const authorization = req.headers.authorization;
11
+ if (!token && authorization) {
12
+ token = authorization?.split?.('Bearer ')?.[1] || '';
13
+ }
10
14
 
11
15
  let data = jwt.token2data(token);
12
16
  if (data) {
13
17
  let { userid, exp } = data;
14
- req.headers['x-userid'] = userid;
15
- let expires = new Date();
16
- expires.setDate(expires.getDate() + 3);
17
- if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
18
- let token = jwt.generateToken(tokenData);
19
- res.cookie('X-Token', token, { expires });
18
+ if (userid) {
19
+ req.headers['x-userid'] = userid;
20
+ let expires = new Date();
21
+ expires.setDate(expires.getDate() + 3);
22
+ if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
23
+ let token = jwt.generateToken(tokenData);
24
+ res.cookie('X-Token', token, { expires });
25
+ }
20
26
  }
21
27
  return next();
22
28
  }
@@ -39,16 +45,22 @@ async function authCheck(req, res, next) {
39
45
  function checkLogin(req, res, next) {
40
46
  let token = req.cookies['X-Token'] || req.get('X-Token');
41
47
  const ip = req.ip;
48
+ const authorization = req.headers.authorization;
49
+ if (!token && authorization) {
50
+ token = authorization?.split?.('Bearer ')?.[1] || '';
51
+ }
42
52
 
43
53
  let data = jwt.token2data(token);
44
54
  if (data) {
45
55
  let { userid, exp } = data;
46
- req.headers['x-userid'] = userid;
47
- let expires = new Date();
48
- expires.setDate(expires.getDate() + 3);
49
- if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
50
- let token = jwt.generateToken(tokenData);
51
- res.cookie('X-Token', token, { expires });
56
+ if (userid) {
57
+ req.headers['x-userid'] = userid;
58
+ let expires = new Date();
59
+ expires.setDate(expires.getDate() + 3);
60
+ if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
61
+ let token = jwt.generateToken(tokenData);
62
+ res.cookie('X-Token', token, { expires });
63
+ }
52
64
  }
53
65
  return next();
54
66
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gingkoo/base-server",
3
- "version": "0.0.1-alpha.5",
3
+ "version": "0.0.1-alpha.7",
4
4
  "description": "",
5
5
  "main": "app.js",
6
6
  "scripts": {
@@ -22,14 +22,15 @@
22
22
  "license": "ISC",
23
23
  "dependencies": {
24
24
  "@gingkoo/node-tools": "^1.1.0-alpha.0",
25
+ "axios": "1.6.8",
25
26
  "bcryptjs": "^2.4.3",
26
27
  "chalk": "^4.1.2",
27
28
  "compression": "^1.7.4",
28
29
  "connect-timeout": "^1.9.0",
29
30
  "cookie-parser": "^1.4.6",
30
31
  "date-fns": "^2.29.3",
31
- "express": "^4.19.1",
32
32
  "dayjs": "^1.11.6",
33
+ "express": "^4.19.1",
33
34
  "express-xml-bodyparser": "^0.3.0",
34
35
  "getavataaars": "^1.0.0",
35
36
  "http-proxy-middleware": "^2.0.1",