@azteam/express 1.2.216 → 1.2.219

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/express",
3
- "version": "1.2.216",
3
+ "version": "1.2.219",
4
4
  "main": "src/index.js",
5
5
  "engines": {
6
6
  "node": ">= 12.0.0",
package/src/Server.js CHANGED
@@ -10,9 +10,7 @@ import morgan from 'morgan';
10
10
  import cors from 'cors';
11
11
  import _ from 'lodash';
12
12
  import 'express-async-errors';
13
- import {CORS, errorCatch, ErrorException, UNKNOWN} from '@azteam/error';
14
-
15
- import {authMiddleware} from './middleware/authMiddleware';
13
+ import {CORS, errorCatch, ErrorException, NOT_FOUND, UNKNOWN} from '@azteam/error';
16
14
 
17
15
  const RES_TYPE = {
18
16
  ARRAY: 'ARRAY',
@@ -269,12 +267,10 @@ class Server {
269
267
  next();
270
268
  });
271
269
 
272
- _.map(this.middlewares, (middleware) => {
270
+ _.map(this.middlewares, function (middleware) {
273
271
  app.use(middleware);
274
272
  });
275
273
 
276
- app.use(authMiddleware);
277
-
278
274
  const msg = [];
279
275
  _.map(this.controllers, (data) => {
280
276
  const {controller} = data;
@@ -307,11 +303,15 @@ class Server {
307
303
 
308
304
  console.table(msg);
309
305
 
310
- app.all('/', async (req, res) => {
306
+ app.all('/', async function (req, res) {
311
307
  return res.success('welcome');
312
308
  });
313
309
 
314
- app.use((err, req, res, next) => {
310
+ app.use(function (req, res) {
311
+ throw new ErrorException(NOT_FOUND);
312
+ });
313
+
314
+ app.use(function (err, req, res, next) {
315
315
  const error = errorCatch(err);
316
316
 
317
317
  if (process.env.NODE_ENV === 'development') {
@@ -1,5 +1,5 @@
1
1
  import jwt from 'jsonwebtoken';
2
- import {ErrorException, TOKEN_EXPIRED, TOKEN_FAILED, UNKNOWN} from '@azteam/error';
2
+ import {ErrorException, TOKEN_EXPIRED, TOKEN_FAILED} from '@azteam/error';
3
3
 
4
4
  function systemLogin(userData = null) {
5
5
  let user = {};
@@ -11,35 +11,42 @@ function systemLogin(userData = null) {
11
11
  return user;
12
12
  }
13
13
 
14
- export function authMiddleware(req, res, next) {
15
- const {headers} = req;
14
+ export default function (cbLoginAPI) {
15
+ return async function (req, res, next) {
16
+ const {headers} = req;
16
17
 
17
- if (headers['x-app-secret'] === process.env.SECRET_KEY) {
18
- req.user = systemLogin(headers['x-app-user']);
19
- } else {
20
- let token = null;
18
+ if (headers['x-app-secret'] === process.env.SECRET_KEY) {
19
+ req.user = systemLogin(headers['x-app-user']);
20
+ } else {
21
+ let token = null;
21
22
 
22
- if (headers.authorization) {
23
- token = headers.authorization;
24
- }
23
+ if (headers.authorization) {
24
+ token = headers.authorization;
25
+ }
25
26
 
26
- if (token) {
27
- if (token.startsWith('JWT ')) {
28
- token = token.replace('JWT ', '');
27
+ if (token) {
28
+ if (token.startsWith('Bearer ')) {
29
+ token = token.replace('Bearer ', '');
29
30
 
30
- try {
31
- req.user = jwt.verify(token, process.env.SECRET_KEY);
32
- return next();
33
- } catch (err) {
34
- if (err.name === 'TokenExpiredError') {
35
- throw new ErrorException(TOKEN_EXPIRED, err);
31
+ try {
32
+ req.user = jwt.verify(token, process.env.SECRET_KEY);
33
+ return next();
34
+ } catch (err) {
35
+ if (err.name === 'TokenExpiredError') {
36
+ throw new ErrorException(TOKEN_EXPIRED, err);
37
+ }
38
+ throw new ErrorException(TOKEN_FAILED, err);
36
39
  }
37
- throw new ErrorException(UNKNOWN, err);
40
+ } else {
41
+ const data = await cbLoginAPI(token);
42
+ if (data) {
43
+ req.user = data;
44
+ }
45
+ return next();
38
46
  }
39
47
  }
40
- throw new ErrorException(TOKEN_FAILED, ['Token type failed']);
41
48
  }
42
- }
43
49
 
44
- return next();
50
+ return next();
51
+ };
45
52
  }
@@ -1,5 +1,6 @@
1
1
  export {default as signMiddleware} from './signMiddleware';
2
2
  export {default as etagMiddleware} from './etagMiddleware';
3
+ export {default as authMiddleware} from './authMiddleware';
3
4
  export {default as roleMiddleware} from './roleMiddleware';
4
5
  export {default as adminRoleMiddleware} from './adminRoleMiddleware';
5
6
  export {default as systemRoleMiddleware} from './systemRoleMiddleware';