@modular-rest/server 1.6.5 → 1.6.6

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.6.5",
3
+ "version": "1.6.6",
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": {
@@ -1,14 +1,14 @@
1
- var mongoose = require('mongoose');
1
+ var mongoose = require("mongoose");
2
2
  var Schema = mongoose.Schema;
3
3
 
4
4
  module.exports = {
5
- 'file': new Schema({
6
- originalName: String,
7
- fileName: String,
8
- owner: String,
9
- format: String,
10
- // Tag being used as the parent dir for files
11
- // uploaddir/$format/$tag/timestamp.format
12
- tag: String,
13
- })
14
- }
5
+ file: new Schema({
6
+ originalName: String,
7
+ fileName: String,
8
+ owner: String,
9
+ format: String,
10
+ // Tag being used as the parent dir for files
11
+ // uploadDir/$format/$tag/timestamp.format
12
+ tag: String,
13
+ }),
14
+ };
package/src/index.js CHANGED
@@ -16,6 +16,7 @@ const DatabaseTrigger = require("./class/database_trigger");
16
16
  const SecurityClass = require("./class/security");
17
17
 
18
18
  const middleware = require("./middlewares");
19
+ const userManager = require("./services/user_manager/service");
19
20
 
20
21
  module.exports = {
21
22
  createRest,
@@ -54,4 +55,6 @@ module.exports = {
54
55
 
55
56
  // Middlewares
56
57
  middleware,
58
+
59
+ userManager: userManager.main,
57
60
  };
@@ -8,13 +8,19 @@ class UserManager {
8
8
  }
9
9
 
10
10
  /**
11
- *
12
- * @param {function} method a method that return random verification code
11
+ * Set a custom method for generating verification codes.
12
+ * @param {function} method - A method that returns a random verification code.
13
13
  */
14
14
  setCustomVerificationCodeGeneratorMethod(method) {
15
15
  this.verificationCodeGeneratorMethod = method;
16
16
  }
17
17
 
18
+ /**
19
+ * Generate a verification code.
20
+ * @param {string} id - The ID for which to generate the verification code.
21
+ * @param {string} idType - The type of the ID.
22
+ * @returns {string} The generated verification code.
23
+ */
18
24
  generateVerificationCode(id, idType) {
19
25
  if (this.verificationCodeGeneratorMethod)
20
26
  return this.verificationCodeGeneratorMethod(id, idType);
@@ -24,9 +30,10 @@ class UserManager {
24
30
  }
25
31
 
26
32
  /**
27
- * Get a user by its Id.
28
- *
29
- * @param {string} id userid
33
+ * Get a user by their ID.
34
+ * @param {string} id - The ID of the user.
35
+ * @returns {Promise<User>} A promise that resolves to the user.
36
+ * @throws {string} If the user is not found.
30
37
  */
31
38
  getUserById(id) {
32
39
  return new Promise(async (done, reject) => {
@@ -39,7 +46,10 @@ class UserManager {
39
46
  .exec()
40
47
  .catch(reject);
41
48
 
42
- if (!userDoc) reject("user not found");
49
+ if (!userDoc) {
50
+ reject("user not found");
51
+ return;
52
+ }
43
53
 
44
54
  let user = User.loadFromModel(userDoc);
45
55
  done(user);
@@ -47,9 +57,43 @@ class UserManager {
47
57
  }
48
58
 
49
59
  /**
50
- * Get user by token.
51
- *
52
- * @param {string} token auth token
60
+ * Get a user by their identity.
61
+ * @param {string} id - The identity of the user.
62
+ * @param {string} idType - The type of the identity (phone or email).
63
+ * @returns {Promise<User>} A promise that resolves to the user.
64
+ * @throws {string} If the user is not found.
65
+ */
66
+ getUserByIdentity(id, idType) {
67
+ return new Promise(async (done, reject) => {
68
+ let userModel = DataProvider.getCollection("cms", "auth");
69
+
70
+ let query = {};
71
+
72
+ if (idType == "phone") query["phone"] = id;
73
+ else if (idType == "email") query["email"] = id;
74
+
75
+ let userDoc = await userModel
76
+ .findOne(query)
77
+ .select({ password: 0 })
78
+ .populate("permission")
79
+ .exec()
80
+ .catch(reject);
81
+
82
+ if (!userDoc) {
83
+ reject("user not found");
84
+ return;
85
+ }
86
+
87
+ let user = User.loadFromModel(userDoc);
88
+ done(user);
89
+ });
90
+ }
91
+
92
+ /**
93
+ * Get a user by their token.
94
+ * @param {string} token - The token of the user.
95
+ * @returns {Promise<User>} A promise that resolves to the user.
96
+ * @throws {string} If the user has a wrong permission.
53
97
  */
54
98
  getUserByToken(token) {
55
99
  return JWT.main.verify(token).then(async (payload) => {
@@ -67,10 +111,10 @@ class UserManager {
67
111
  }
68
112
 
69
113
  /**
70
- * Define whether or not verification code is valid.
71
- *
72
- * @param {string} id auth id phone|email
73
- * @param {string} code verification code
114
+ * Check if a verification code is valid.
115
+ * @param {string} id - The ID of the user.
116
+ * @param {string} code - The verification code.
117
+ * @returns {boolean} Whether the verification code is valid.
74
118
  */
75
119
  isCodeValid(id, code) {
76
120
  let key = false;
@@ -85,23 +129,24 @@ class UserManager {
85
129
  }
86
130
 
87
131
  /**
88
- * Login and return user token.
89
- *
90
- * @param {string} id auth id phone|email
91
- * @param {string} idType auth type phone|email
92
- * @param {string} password
132
+ * Login a user and return their token.
133
+ * @param {string} id - The ID of the user.
134
+ * @param {string} idType - The type of the ID (phone or email).
135
+ * @param {string} password - The password of the user.
136
+ * @returns {Promise<string>} A promise that resolves to the token of the user.
137
+ * @throws {string} If the user is not found.
93
138
  */
94
139
  loginUser(id = "", idType = "", password = "") {
95
140
  let token;
96
141
 
97
142
  return new Promise(async (done, reject) => {
98
143
  // Get user model
99
- let userModel = DataProvider.getCollection("cms", "auth");
144
+ const userModel = DataProvider.getCollection("cms", "auth");
100
145
 
101
146
  /**
102
147
  * Setup query to find by phone or email
103
148
  */
104
- let query = {
149
+ const query = {
105
150
  password: Buffer.from(password).toString("base64"),
106
151
  type: "user",
107
152
  };
@@ -110,7 +155,7 @@ class UserManager {
110
155
  else if (idType == "email") query["email"] = id;
111
156
 
112
157
  // Get from database
113
- let gottenFromDB = await userModel
158
+ const gottenFromDB = await userModel
114
159
  .findOne(query)
115
160
  .populate("permission")
116
161
  .exec()
@@ -120,11 +165,13 @@ class UserManager {
120
165
  // Token
121
166
  else {
122
167
  // Load user
123
- let user = await User.loadFromModel(gottenFromDB).then().catch(reject);
168
+ const user = await User.loadFromModel(gottenFromDB)
169
+ .then()
170
+ .catch(reject);
124
171
 
125
172
  // Get token payload
126
173
  // This is some information about the user.
127
- let payload = user.getBrief();
174
+ const payload = user.getBrief();
128
175
 
129
176
  // Generate json web token
130
177
  token = await JWT.main.sign(payload).then().catch(reject);
@@ -135,7 +182,40 @@ class UserManager {
135
182
  }
136
183
 
137
184
  /**
138
- * Login as anonymous user
185
+ * Issue a token for a user.
186
+ * @param {string} email - The email of the user.
187
+ * @returns {Promise<string>} A promise that resolves to the token of the user.
188
+ * @throws {string} If the user is not found.
189
+ */
190
+ issueTokenForUser(email) {
191
+ return new Promise(async (done, reject) => {
192
+ const userModel = DataProvider.getCollection("cms", "auth");
193
+ const query = { email: email };
194
+
195
+ // Get from database
196
+ const gottenFromDB = await userModel
197
+ .findOne(query)
198
+ .populate("permission")
199
+ .exec()
200
+ .catch(reject);
201
+
202
+ if (!gottenFromDB) reject("user not found");
203
+
204
+ const user = await User.loadFromModel(gottenFromDB).then().catch(reject);
205
+
206
+ // Get token payload
207
+ // This is some information about the user.
208
+ const payload = user.getBrief();
209
+
210
+ // Generate json web token
211
+ await JWT.main.sign(payload).then(done).catch(reject);
212
+ });
213
+ }
214
+
215
+ /**
216
+ * Login as an anonymous user.
217
+ * @returns {Promise<string>} A promise that resolves to the token of the anonymous user.
218
+ * @throws {string} If the anonymous user is not found.
139
219
  */
140
220
  loginAnonymous() {
141
221
  let token;
@@ -180,16 +260,22 @@ class UserManager {
180
260
  }
181
261
 
182
262
  /**
183
- * Store user email|phone temporarily in memory.
184
- *
185
- * @param {string} id id is username or phone number
186
- * @param {string} type type is the type of id
187
- * @param {string} code code is a string being sent to user and he/she must return it back.
263
+ * Register a temporary ID.
264
+ * @param {string} id - The ID to register.
265
+ * @param {string} type - The type of the ID.
266
+ * @param {string} code - The verification code.
188
267
  */
189
268
  registerTemporaryID(id, type, code) {
190
269
  this.tempIds[id] = { id: id, type: type, code: code };
191
270
  }
192
271
 
272
+ /**
273
+ * Submit a password for a temporary ID.
274
+ * @param {string} id - The ID.
275
+ * @param {string} password - The password.
276
+ * @param {string} code - The verification code.
277
+ * @returns {Promise<boolean>} A promise that resolves to whether the operation was successful.
278
+ */
193
279
  async submitPasswordForTemporaryID(id, password, code) {
194
280
  let key = false;
195
281
 
@@ -213,6 +299,13 @@ class UserManager {
213
299
  return key;
214
300
  }
215
301
 
302
+ /**
303
+ * Change the password for a temporary ID.
304
+ * @param {string} id - The ID.
305
+ * @param {string} password - The new password.
306
+ * @param {string} code - The verification code.
307
+ * @returns {Promise<boolean>} A promise that resolves to whether the operation was successful.
308
+ */
216
309
  async changePasswordForTemporaryID(id, password, code) {
217
310
  let key = false;
218
311
 
@@ -231,6 +324,12 @@ class UserManager {
231
324
  return key;
232
325
  }
233
326
 
327
+ /**
328
+ * Register a user.
329
+ * @param {Object} detail - The details of the user.
330
+ * @returns {Promise<string>} A promise that resolves to the ID of the new user.
331
+ * @throws {string} If the user could not be registered.
332
+ */
234
333
  registerUser(detail) {
235
334
  return new Promise(async (done, reject) => {
236
335
  // get default permission
@@ -263,6 +362,12 @@ class UserManager {
263
362
  });
264
363
  }
265
364
 
365
+ /**
366
+ * Change the password of a user.
367
+ * @param {Object} query - The query to find the user.
368
+ * @param {string} newPass - The new password.
369
+ * @returns {Promise<void>} A promise that resolves when the operation is complete.
370
+ */
266
371
  changePassword(query, newPass) {
267
372
  let update = { $set: { password: newPass } };
268
373
  let authM = DataProvider.getCollection("cms", "auth");