@kne/fastify-account 2.0.4 → 2.0.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/libs/controllers/account.js +8 -6
- package/libs/models/user.js +8 -2
- package/libs/services/account.js +30 -6
- package/libs/services/user.js +30 -6
- package/package.json +1 -1
|
@@ -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
|
-
|
|
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,
|
|
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`, {
|
|
@@ -235,18 +235,20 @@ const accountController = fp(async (fastify, options) => {
|
|
|
235
235
|
tags: ['账号'], summary: '忘记密码', body: {
|
|
236
236
|
oneOf: [{
|
|
237
237
|
type: 'object', required: ['email'], properties: {
|
|
238
|
-
email: { type: 'string', description: '邮箱' }
|
|
238
|
+
email: { type: 'string', description: '邮箱' }, referer: { type: 'string', description: '来源' }
|
|
239
239
|
}
|
|
240
240
|
}, {
|
|
241
241
|
type: 'object', required: ['phone'], properties: {
|
|
242
|
-
phone: { type: 'string', description: '手机号' }
|
|
242
|
+
phone: { type: 'string', description: '手机号' }, referer: { type: 'string', description: '来源' }
|
|
243
243
|
}
|
|
244
244
|
}]
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
247
|
}, async request => {
|
|
248
248
|
const name = request.body.email || request.body.phone;
|
|
249
|
-
const token = await services.account.sendJWTVerificationCode({
|
|
249
|
+
const token = await services.account.sendJWTVerificationCode({
|
|
250
|
+
name, type: 5, options: { referer: request.body.referer }
|
|
251
|
+
});
|
|
250
252
|
return options.isTest ? { token } : {};
|
|
251
253
|
});
|
|
252
254
|
|
package/libs/models/user.js
CHANGED
|
@@ -26,9 +26,15 @@ const user = ({ DataTypes, definePrimaryType }) => {
|
|
|
26
26
|
}
|
|
27
27
|
}, options: {
|
|
28
28
|
indexes: [{
|
|
29
|
-
unique: true, fields: ['email',
|
|
29
|
+
unique: true, fields: ['email'],
|
|
30
|
+
where: {
|
|
31
|
+
deleted_at: null
|
|
32
|
+
}
|
|
30
33
|
}, {
|
|
31
|
-
unique: true, fields: ['phone',
|
|
34
|
+
unique: true, fields: ['phone'],
|
|
35
|
+
where: {
|
|
36
|
+
deleted_at: null
|
|
37
|
+
}
|
|
32
38
|
}]
|
|
33
39
|
}
|
|
34
40
|
};
|
package/libs/services/account.js
CHANGED
|
@@ -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
|
|
package/libs/services/user.js
CHANGED
|
@@ -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,
|
|
151
|
+
getUserInstance,
|
|
152
|
+
getUserInstanceByName,
|
|
153
|
+
getUser,
|
|
154
|
+
addUser,
|
|
155
|
+
saveUser,
|
|
156
|
+
accountIsExists,
|
|
157
|
+
getUserList,
|
|
158
|
+
setSuperAdmin,
|
|
159
|
+
setUserStatus
|
|
136
160
|
};
|
|
137
161
|
});
|
|
138
162
|
|