free-be-account 0.0.9 → 0.0.11

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/index.js CHANGED
@@ -152,6 +152,38 @@ const verify_api_permission = async (app, mdl, user, api_path) => {
152
152
  return true; // TODO: secure enough??
153
153
  }
154
154
 
155
+
156
+ async function clear_cache_token_by_user_id (app, id) {
157
+ if (!id) return;
158
+
159
+ const cacheKeys = await app.cache.keys();
160
+ if (cacheKeys && cacheKeys.length) {
161
+ for (let i = 0; i < cacheKeys.length; i += 1) {
162
+ const k = cacheKeys[i];
163
+
164
+ let value = await app.cache.get(k);
165
+ if (value && value.userId && value.userId === id)
166
+ await app.cache.del(k);
167
+ }
168
+ }
169
+ }
170
+
171
+ async function generate_new_access_token_pwd (app, userId, oldToken, keepToken = '', isWx = false) {
172
+ let uuid = keepToken || uuidv1();
173
+
174
+ // remove the old one from cache
175
+ app.cache.del(oldToken);
176
+ // cache.del(oldToken);
177
+ await clear_cache_token_by_user_id(app, userId);
178
+
179
+ // add the new one to the cache
180
+
181
+ app.cache.put(uuid, { userId: userId, type: isWx ? 'wx' : 'pwd' }, app.config['cacheTimeout']);
182
+ // cache.put(uuid, { userId: userId, type: 'pwd' }, app.config['cacheTimeout']);
183
+
184
+ return uuid;
185
+ }
186
+
155
187
  module.exports = (app) => ({
156
188
  sms: sms(app),
157
189
  AccountAuditStatus,
@@ -545,6 +577,8 @@ module.exports = (app) => ({
545
577
  ks.forEach(k => app.cache.del(k))
546
578
  });
547
579
  },
580
+ clear_cache_token_by_user_id,
581
+ generate_new_access_token_pwd,
548
582
  hooks: {
549
583
  onBegin: (app) => {
550
584
  app.use(passport.initialize());
@@ -833,7 +867,7 @@ module.exports = (app) => ({
833
867
  app.models['account'].create({
834
868
  Enabled: true,
835
869
  Deleted: false,
836
- Permission: app.config.passport.accountDefaultPermissions || {},
870
+ Permission: app.config.account.accountDefaultPermissions || {},
837
871
  Profile: profile,
838
872
  }).then((nuser) => {
839
873
  if (nuser) {
@@ -985,37 +1019,6 @@ module.exports = (app) => ({
985
1019
  return next();
986
1020
  });
987
1021
 
988
- async function clear_cache_token_by_user_id (id) {
989
- if (!id) return;
990
-
991
- const cacheKeys = await app.cache.keys();
992
- if (cacheKeys && cacheKeys.length) {
993
- for (let i = 0; i < cacheKeys.length; i += 1) {
994
- const k = cacheKeys[i];
995
-
996
- let value = await app.cache.get(k);
997
- if (value && value.userId && value.userId === id)
998
- await app.cache.del(k);
999
- }
1000
- }
1001
- }
1002
-
1003
- async function generate_new_access_token_pwd (userId, oldToken, keepToken = '', isWx = false) {
1004
- let uuid = keepToken || uuidv1();
1005
-
1006
- // remove the old one from cache
1007
- app.cache.del(oldToken);
1008
- // cache.del(oldToken);
1009
- await clear_cache_token_by_user_id(userId);
1010
-
1011
- // add the new one to the cache
1012
-
1013
- app.cache.put(uuid, { userId: userId, type: isWx ? 'wx' : 'pwd' }, app.config['cacheTimeout']);
1014
- // cache.put(uuid, { userId: userId, type: 'pwd' }, app.config['cacheTimeout']);
1015
-
1016
- return uuid;
1017
- }
1018
-
1019
1022
  // login with the specified strategy
1020
1023
  app.post(`${app.config['baseUrl'] || ''}/login`,
1021
1024
  passport.authenticate(m.config['strategy'] || 'local', { session: false }),
@@ -1044,10 +1047,10 @@ module.exports = (app) => ({
1044
1047
  (req.user && req.user.PhoneNumber && m.config['keepTokenAccounts'].indexOf(req.user.PhoneNumber) >= 0)) {
1045
1048
  // keep token
1046
1049
  const kt = await app.cache.get(`_keep_token_${req.user.id}`);
1047
- token = await generate_new_access_token_pwd(req.user.id, access_token, kt, req.user.isWx);
1050
+ token = await generate_new_access_token_pwd(app, req.user.id, access_token, kt, req.user.isWx);
1048
1051
  app.cache.set(`_keep_token_${req.user.id}`, token);
1049
1052
  } else {
1050
- token = await generate_new_access_token_pwd(req.user.id, access_token, null, req.user.isWx);
1053
+ token = await generate_new_access_token_pwd(app, req.user.id, access_token, null, req.user.isWx);
1051
1054
  }
1052
1055
 
1053
1056
  res.cookie('token', token, { maxAge: app.config['cookieTimeout'] });
@@ -1118,21 +1121,23 @@ module.exports = (app) => ({
1118
1121
  const phone = crypto.encoder.desDecode(req.body.PhoneNumber, m.config.desKey);
1119
1122
 
1120
1123
  // check user existance if necessary
1121
- const existsCount = await res.app.models.account.countDocuments({$or: [
1122
- { PhoneNumber: phone },
1123
- { 'Profile.Email': phone },
1124
- ]});
1125
-
1126
- if (req.body.exists && existsCount <= 0) {
1127
- res.makeError(409, 'User not exists!', m);
1128
- return next('route');
1129
- }
1130
- if (!req.body.exists && existsCount > 0) {
1131
- res.makeError(410, 'User aleady exists!', m);
1132
- return next('route');
1124
+ if (req.body.exists !== 'all') {
1125
+ const existsCount = await res.app.models.account.countDocuments({$or: [
1126
+ { PhoneNumber: phone },
1127
+ { 'Profile.Email': phone },
1128
+ ]});
1129
+
1130
+ if (req.body.exists && existsCount <= 0) {
1131
+ res.makeError(409, 'User not exists!', m);
1132
+ return next('route');
1133
+ }
1134
+ if (!req.body.exists && existsCount > 0) {
1135
+ res.makeError(410, 'User aleady exists!', m);
1136
+ return next('route');
1137
+ }
1133
1138
  }
1134
1139
 
1135
- const result = await res.Module('sms').sendRandom(phone, undefined, true, req.body.smsTemp || 'register');
1140
+ const result = await m.sms.sendRandom(phone, undefined, true, req.body.smsTemp || 'register');
1136
1141
 
1137
1142
  if (!result) {
1138
1143
  res.makeError(500, 'Failed to send sms!', m);
@@ -1147,7 +1152,6 @@ module.exports = (app) => ({
1147
1152
  return next();
1148
1153
  })
1149
1154
 
1150
-
1151
1155
  // verfiy the sms code
1152
1156
  app.post(`${(app.config['baseUrl'] || '')}/register/verify`, async (req, res, next) => {
1153
1157
  if (!req.body.PhoneNumber || !req.body.code) {
@@ -1155,7 +1159,7 @@ module.exports = (app) => ({
1155
1159
  return next('route');
1156
1160
  }
1157
1161
  const phone = crypto.encoder.desDecode(req.body.PhoneNumber, m.config.desKey);
1158
- const result = await res.Module('sms').verify(phone, req.body.code);
1162
+ const result = await m.sms.verify(phone, req.body.code);
1159
1163
  // app.logger.debug(cache.exportJson());
1160
1164
 
1161
1165
  if (!result) {
@@ -1167,7 +1171,6 @@ module.exports = (app) => ({
1167
1171
  return next();
1168
1172
  })
1169
1173
 
1170
-
1171
1174
  // verify phone number (duplication) for register
1172
1175
  app.post(`${(app.config['baseUrl'] || '')}/register/verify/phone`, async (req, res, next) => {
1173
1176
  if (!req.body.PhoneNumber) {
@@ -1196,7 +1199,7 @@ module.exports = (app) => ({
1196
1199
  return next('route');
1197
1200
  }
1198
1201
 
1199
- const result = await res.Module('sms').verify(phone, req.body.code);
1202
+ const result = await m.sms.verify(phone, req.body.code);
1200
1203
 
1201
1204
  if (!result) {
1202
1205
  res.makeError(403, 'Code verification failed!', m);
@@ -1269,7 +1272,7 @@ module.exports = (app) => ({
1269
1272
  const phone = crypto.encoder.desDecode(req.body.PhoneNumber, m.config.desKey);
1270
1273
  const password = crypto.encoder.desDecode(req.body.Password, m.config.desKey);
1271
1274
 
1272
- const result = await res.Module('sms').verify(phone, req.body.code);
1275
+ const result = await m.sms.verify(phone, req.body.code);
1273
1276
 
1274
1277
  if (!result) {
1275
1278
  res.makeError(403, 'Code verification failed!', m);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "free-be-account",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "main": "index.js",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -79,11 +79,11 @@ router.post('/submit',
79
79
  res.locals.body.Profile = {...user.Profile, ...req.body.Profile};
80
80
  }
81
81
 
82
- res.locals.body.Status = res.app.modules.passport.AccountAuditStatus.Auditing;
82
+ res.locals.body.Status = res.app.modules.account.AccountAuditStatus.Auditing;
83
83
 
84
84
  // set to default permission
85
- const p = res.app.modules.passport.config.accountDefaultPermissions;
86
- res.app.modules.passport.utils.clearPermission(p);
85
+ const p = res.app.modules.account.config.accountDefaultPermissions;
86
+ res.app.modules.account.utils.clearPermission(p);
87
87
  res.locals.body.Permission = p;
88
88
 
89
89
  res.locals.filters = { id: req.user.id };
@@ -24,9 +24,9 @@ router.put('/',
24
24
 
25
25
  // update phone number
26
26
  res.locals.body = {};
27
- res.locals.body.PhoneNumber = res.app.modules.passport.utils.crypto.encoder.desDecode(req.body.phone, res.app.modules.passport.config.desKey);
27
+ res.locals.body.PhoneNumber = res.app.modules.account.utils.crypto.encoder.desDecode(req.body.phone, res.app.modules.account.config.desKey);
28
28
 
29
- const oResult = await res.Module('sms').verify(ophone, req.body.ocode);
29
+ const oResult = await router.mdl.sms.verify(ophone, req.body.ocode);
30
30
  if (!oResult) {
31
31
  res.makeError(400, 'Verification code for the old phone is incorrect!', router.mdl);
32
32
  await res.app.cache.del(ophone);
@@ -34,7 +34,7 @@ router.put('/',
34
34
  return next('route');
35
35
  }
36
36
 
37
- const result = await res.Module('account').sms.verify(res.locals.body.PhoneNumber, req.body.code);
37
+ const result = await router.mdl.sms.verify(res.locals.body.PhoneNumber, req.body.code);
38
38
  if (!result) {
39
39
  res.makeError(405, 'Verification code for the new phone is incorrect!', router.mdl);
40
40
  await res.app.cache.del(ophone);
@@ -16,7 +16,7 @@ router.put('/',
16
16
  return next('route');
17
17
  }
18
18
 
19
- const result = await res.Module('sms').verify(phone, req.body.code);
19
+ const result = await router.mdl.sms.verify(phone, req.body.code);
20
20
  // app.logger.debug(cache.exportJson());
21
21
 
22
22
  if (!result) {
@@ -32,7 +32,7 @@ router.put('/',
32
32
  const password = res.app.modules.account.utils.crypto.encoder.desDecode(req.body.Password, res.app.modules.account.config.desKey);
33
33
 
34
34
  res.locals.body = {};
35
- res.locals.body.Password = res.app.modules.passport.utils.encryptPwd(password, res.app.modules.passport.config.pwdEncryptMethod || 'md5');
35
+ res.locals.body.Password = res.app.modules.account.utils.encryptPwd(password, res.app.modules.account.config.pwdEncryptMethod || 'md5');
36
36
 
37
37
  res.locals.filters = { id: req.user.id };
38
38
  res.locals.fields = [
@@ -79,7 +79,6 @@ router.post('/',
79
79
  // also same Org (but should check whether we have Org module??)
80
80
  if (req.user.Org) req.body.Org = req.user.Org;
81
81
 
82
- // TODO: should not set status here as we don't have this field yet (which was added in passport)
83
82
  req.body.Status = '1';
84
83
 
85
84
  // TODO: check permission, should not be bigger than the main account
package/sms/index.js CHANGED
@@ -5,8 +5,8 @@ const nodemailer = require('nodemailer');
5
5
 
6
6
  let global;
7
7
 
8
- if (fs.existsSync(path.resolve(__dirname, '../../global.js'))) {
9
- global = require('../../global');
8
+ if (fs.existsSync(path.resolve(__dirname, '../../../global.js'))) {
9
+ global = require('../../../global');
10
10
  }
11
11
 
12
12
  let MAIL_TRANS = undefined;