@kne/fastify-account 2.0.3 → 2.0.5

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
@@ -68,6 +68,7 @@ const user = fp(
68
68
  if (!(await services.admin.checkIsSuperAdmin(request.userInfo))) {
69
69
  throw Unauthorized('不能执行该操作,需要超级管理员权限');
70
70
  }
71
+ request.userInfo.isAdmin = true;
71
72
  }
72
73
  }
73
74
  ]
@@ -177,7 +177,7 @@ const accountController = fp(async (fastify, options) => {
177
177
  schema: {
178
178
  type: 'object', properties: {
179
179
  token: { type: 'string', description: '用户token' },
180
- currentTenantId: { type: 'string', description: '当前租户id' }
180
+ status: { type: 'number', description: '用户当前状态' }
181
181
  }
182
182
  }
183
183
  }
@@ -187,8 +187,8 @@ const accountController = fp(async (fastify, options) => {
187
187
  }
188
188
  }, async request => {
189
189
  const appName = request.headers['x-app-name'];
190
- const { token, user } = await services.account.login(Object.assign({}, request.body, { appName }));
191
- return { token, currentTenantId: user.currentTenantId };
190
+ const { token, user, status } = await services.account.login(Object.assign({}, request.body, { appName }));
191
+ return { token, status };
192
192
  });
193
193
 
194
194
  fastify.post(`${options.prefix}/account/modifyPassword`, {
@@ -26,9 +26,15 @@ const user = ({ DataTypes, definePrimaryType }) => {
26
26
  }
27
27
  }, options: {
28
28
  indexes: [{
29
- unique: true, fields: ['email', 'deleted_at']
29
+ unique: true, fields: ['email'],
30
+ where: {
31
+ deleted_at: null
32
+ }
30
33
  }, {
31
- unique: true, fields: ['phone', 'deleted_at']
34
+ unique: true, fields: ['phone'],
35
+ where: {
36
+ deleted_at: null
37
+ }
32
38
  }]
33
39
  }
34
40
  };
@@ -115,11 +115,7 @@ const accountService = fp(async (fastify, options) => {
115
115
  };
116
116
 
117
117
  const login = async ({ type, email, phone, password }) => {
118
- const query = {
119
- status: {
120
- [Op.or]: [0, 1]
121
- }
122
- };
118
+ const query = {};
123
119
  (() => {
124
120
  if (type === 'email') {
125
121
  query.email = email.toLowerCase();
@@ -142,12 +138,38 @@ const accountService = fp(async (fastify, options) => {
142
138
 
143
139
  await passwordAuthentication({ accountId: user.userAccountId, password });
144
140
 
141
+ if (!(user.status === 0 || user.status === 1)) {
142
+ return {
143
+ status: user.status
144
+ };
145
+ }
146
+
145
147
  return {
146
148
  token: fastify.jwt.sign({ payload: { id: user.id } }),
147
149
  user: Object.assign({}, user.get({ plain: true }), { id: user.id })
148
150
  };
149
151
  };
150
152
 
153
+ const resetPasswordByToken = async ({ password, token }) => {
154
+ const { name } = await verificationJWTCodeValidate({ token });
155
+ const user = await services.user.getUserInstanceByName({ name, status: [0, 1] });
156
+ await resetPassword({ password, userId: user.id });
157
+ };
158
+
159
+ const modifyPassword = async ({ email, phone, oldPwd, newPwd }) => {
160
+ const user = await services.user.getUserInstanceByName({ name: email || phone, status: 10 });
161
+ if (!user) {
162
+ throw new Error('新用户密码只能初始化一次');
163
+ }
164
+ if (oldPwd === newPwd) {
165
+ throw new Error('重置密码不能和初始化密码相同');
166
+ }
167
+ await passwordAuthentication({ accountId: user.userAccountId, password: oldPwd });
168
+ await resetPassword({ userId: user.id, password: newPwd });
169
+ user.status = 0;
170
+ await user.save();
171
+ };
172
+
151
173
  const resetPassword = async ({ password, userId }) => {
152
174
  const user = await services.user.getUserInstance({ id: userId });
153
175
  const account = await models.userAccount.create(Object.assign({}, await passwordEncryption(password), {
@@ -167,7 +189,9 @@ const accountService = fp(async (fastify, options) => {
167
189
  login,
168
190
  userNameIsEmail,
169
191
  md5,
170
- resetPassword
192
+ resetPassword,
193
+ resetPasswordByToken,
194
+ modifyPassword
171
195
  };
172
196
  });
173
197
 
@@ -1,5 +1,5 @@
1
1
  const fp = require('fastify-plugin');
2
- const { pick, get } = require('lodash');
2
+ const { pick, get, isNil } = require('lodash');
3
3
  const httpErrors = require('http-errors');
4
4
 
5
5
  const { Unauthorized } = httpErrors;
@@ -33,6 +33,25 @@ const userService = fp(async (fastify, options) => {
33
33
  return Object.assign({}, pick(user, ['id', 'avatar', 'nickname', 'phone', 'email', 'gender', 'status', 'birthday', 'description']));
34
34
  };
35
35
 
36
+ const getUserInstanceByName = async ({ name, status }) => {
37
+ const isEmail = services.account.userNameIsEmail(name);
38
+ const query = {};
39
+ if (!isNil(status)) {
40
+ query['status'] = Array.isArray(status) ? {
41
+ [fastify.sequelize.Sequelize.Op.or]: status
42
+ } : status;
43
+ }
44
+ const user = await models.user.findOne({
45
+ where: Object.assign({}, isEmail ? { email: name } : { phone: name }, query)
46
+ });
47
+
48
+ if (!user) {
49
+ throw new Error('用户不存在');
50
+ }
51
+
52
+ return user;
53
+ };
54
+
36
55
  const accountIsExists = async ({ email, phone }, currentUser) => {
37
56
  const query = [];
38
57
  if (email && email !== get(currentUser, 'email')) {
@@ -91,10 +110,7 @@ const userService = fp(async (fastify, options) => {
91
110
  }
92
111
 
93
112
  const { count, rows } = await models.user.findAndCountAll({
94
- where: queryFilter,
95
- offset: perPage * (currentPage - 1),
96
- limit: perPage,
97
- order: [['createdAt', 'DESC']]
113
+ where: queryFilter, offset: perPage * (currentPage - 1), limit: perPage, order: [['createdAt', 'DESC']]
98
114
  });
99
115
  return {
100
116
  pageData: rows.map(item => {
@@ -132,7 +148,15 @@ const userService = fp(async (fastify, options) => {
132
148
  };
133
149
 
134
150
  services.user = {
135
- getUserInstance, getUser, addUser, saveUser, accountIsExists, getUserList, setSuperAdmin, setUserStatus
151
+ getUserInstance,
152
+ getUserInstanceByName,
153
+ getUser,
154
+ addUser,
155
+ saveUser,
156
+ accountIsExists,
157
+ getUserList,
158
+ setSuperAdmin,
159
+ setUserStatus
136
160
  };
137
161
  });
138
162
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "用于用户注册登录认证.",
5
5
  "main": "index.js",
6
6
  "scripts": {