@modular-rest/server 1.11.12 → 1.11.13

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": "@modular-rest/server",
3
- "version": "1.11.12",
3
+ "version": "1.11.13",
4
4
  "description": "a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -7,64 +7,76 @@ let verify = new Router();
7
7
 
8
8
  let service = require('./service').main;
9
9
 
10
- verify.post('/token', async (ctx) =>
11
- {
10
+ verify.post('/token', async (ctx) => {
12
11
  let body = ctx.request.body;
13
12
 
14
13
  // validate result
15
14
  let bodyValidate = validateObject(body, 'token');
16
15
 
17
16
  // fields validation
18
- if(!bodyValidate.isValid)
19
- {
20
- ctx.status = 412;
21
- ctx.body = reply('e', {'e': bodyValidate.requires});
22
- return;
23
- }
17
+ if (!bodyValidate.isValid) {
18
+ ctx.status = 412;
19
+ ctx.body = reply('e', {
20
+ 'e': bodyValidate.requires
21
+ });
22
+ return;
23
+ }
24
24
 
25
25
  await service.verify(body.token)
26
- .then((payload) => ctx.body = reply('s', {'user': payload}))
27
- .catch(err => {
28
- ctx.status = 412;
29
- ctx.body = reply('e', {'e': err});
30
- });
26
+ .then((payload) => ctx.body = reply('s', {
27
+ 'user': payload
28
+ }))
29
+ .catch(err => {
30
+ ctx.status = 412;
31
+ ctx.body = reply('e', {
32
+ 'e': err
33
+ });
34
+ });
31
35
  });
32
36
 
33
- verify.post('/checkAccess', async (ctx) =>
34
- {
37
+ verify.post('/checkAccess', async (ctx) => {
35
38
  let body = ctx.request.body;
36
39
 
37
40
  // validate result
38
41
  let bodyValidate = validateObject(body, 'token permissionField');
39
42
 
40
43
  // fields validation
41
- if(!bodyValidate.isValid)
42
- {
43
- ctx.status = 412;
44
- ctx.body = reply('e', {'e': bodyValidate.requires});
45
- return;
44
+ if (!bodyValidate.isValid) {
45
+ ctx.status = 412;
46
+ ctx.body = reply('e', {
47
+ 'e': bodyValidate.requires
48
+ });
49
+ return;
46
50
  }
47
-
51
+
48
52
  let payload = await service.verify(body.token)
49
53
  .catch(err => {
50
- console.log(err);
51
- ctx.throw(412, err.message);
52
- });
53
-
54
-
54
+ console.log(err);
55
+ ctx.throw(412, err.message);
56
+ });
57
+
58
+
55
59
  let userid = payload.id;
56
-
60
+
57
61
  await global.services.userManager.main.getUserById(userid)
58
- .then((user) =>
59
- {
62
+ .then((user) => {
60
63
  let key = user.hasPermission(body.permissionField);
61
- ctx.body = reply('s', {'access': key});
64
+ ctx.body = reply('s', {
65
+ 'access': key
66
+ });
62
67
  })
63
68
  .catch(err => {
64
69
  ctx.status = 412;
65
- ctx.body = reply('e', {'e': err});
70
+ ctx.body = reply('e', {
71
+ 'e': err
72
+ });
66
73
  });
67
74
  });
68
75
 
76
+ verify.get('/ready', async (ctx) => {
77
+ // it's health check, so return success
78
+ ctx.body = reply('s', {});
79
+ });
80
+
69
81
  module.exports.name = name;
70
82
  module.exports.main = verify;
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Authentication middleware
3
3
  * It checks if incoming request has a valid token in header.authorization
4
+ * Then attaches the user object to ctx.state.user
4
5
  *
5
6
  * @param {Object} ctx - Koa context
6
7
  * @param {Function} next - Koa next function