@kne/fastify-account 2.0.0-alpha.1 → 2.0.0-alpha.3
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 +6 -1
- package/libs/controllers/admin.js +1 -0
- package/libs/controllers/user.js +61 -2
- package/libs/services/user.js +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -48,7 +48,12 @@ const user = fp(
|
|
|
48
48
|
{
|
|
49
49
|
user: async request => {
|
|
50
50
|
const { services } = fastify[options.name];
|
|
51
|
-
|
|
51
|
+
let info;
|
|
52
|
+
try {
|
|
53
|
+
info = await request.jwtVerify();
|
|
54
|
+
} catch (e) {
|
|
55
|
+
throw Unauthorized('身份认证失败');
|
|
56
|
+
}
|
|
52
57
|
//这里判断失效时间
|
|
53
58
|
if (options.jwt.expires && Date.now() - info.iat * 1000 > options.jwt.expires) {
|
|
54
59
|
throw Unauthorized('身份认证超时');
|
|
@@ -36,6 +36,7 @@ const adminController = fp(async (fastify, options) => {
|
|
|
36
36
|
type: 'object',
|
|
37
37
|
properties: {
|
|
38
38
|
id: {type: 'string', description: '用户id'},
|
|
39
|
+
avatar: {type: 'string', description: '头像图片id'},
|
|
39
40
|
nickname: {type: 'string', description: '用户昵称'},
|
|
40
41
|
email: {type: 'string', description: '邮箱'},
|
|
41
42
|
phone: {type: 'string', description: '电话'},
|
package/libs/controllers/user.js
CHANGED
|
@@ -1,17 +1,76 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
|
|
3
3
|
const userController = fp(async (fastify, options) => {
|
|
4
|
-
const { authenticate } = fastify[options.name];
|
|
4
|
+
const { authenticate, services } = fastify[options.name];
|
|
5
5
|
|
|
6
6
|
fastify.get(
|
|
7
7
|
`${options.prefix}/user/getUserInfo`,
|
|
8
8
|
{
|
|
9
|
-
onRequest: [authenticate.user]
|
|
9
|
+
onRequest: [authenticate.user],
|
|
10
|
+
schema: {
|
|
11
|
+
tags: ['用户'],
|
|
12
|
+
summary: '获取用户信息',
|
|
13
|
+
response: {
|
|
14
|
+
200: {
|
|
15
|
+
content: {
|
|
16
|
+
'application/json': {
|
|
17
|
+
schema: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
userInfo: {
|
|
21
|
+
type: 'object',
|
|
22
|
+
properties: {
|
|
23
|
+
id: { type: 'string', description: '用户id' },
|
|
24
|
+
avatar: { type: 'string', description: '头像图片id' },
|
|
25
|
+
nickname: { type: 'string', description: '用户昵称' },
|
|
26
|
+
email: { type: 'string', description: '邮箱' },
|
|
27
|
+
phone: { type: 'string', description: '电话' },
|
|
28
|
+
gender: { type: 'string', description: '性别' },
|
|
29
|
+
birthday: { type: 'string', format: 'date', description: '出生日期' },
|
|
30
|
+
description: { type: 'string', description: '个人简介' },
|
|
31
|
+
status: { type: 'number', description: '状态' }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
10
41
|
},
|
|
11
42
|
async request => {
|
|
12
43
|
return { userInfo: request.userInfo };
|
|
13
44
|
}
|
|
14
45
|
);
|
|
46
|
+
|
|
47
|
+
fastify.post(
|
|
48
|
+
`${options.prefix}/user/saveUserInfo`,
|
|
49
|
+
{
|
|
50
|
+
onRequest: [authenticate.user],
|
|
51
|
+
schema: {
|
|
52
|
+
tags: ['用户'],
|
|
53
|
+
summary: '更新用户信息',
|
|
54
|
+
body: {
|
|
55
|
+
type: 'object',
|
|
56
|
+
properties: {
|
|
57
|
+
avatar: { type: 'string', description: '头像图片id' },
|
|
58
|
+
nickname: { type: 'string', description: '用户昵称' },
|
|
59
|
+
email: { type: 'string', description: '邮箱' },
|
|
60
|
+
phone: { type: 'string', description: '电话' },
|
|
61
|
+
gender: { type: 'string', description: '性别' },
|
|
62
|
+
birthday: { type: 'string', format: 'date', description: '出生日期' },
|
|
63
|
+
description: { type: 'string', description: '个人简介' }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
async request => {
|
|
69
|
+
const { id } = request.authenticatePayload;
|
|
70
|
+
await services.user.saveUser(Object.assign({}, request.body, { id }));
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
);
|
|
15
74
|
});
|
|
16
75
|
|
|
17
76
|
module.exports = userController;
|
package/libs/services/user.js
CHANGED
|
@@ -99,7 +99,7 @@ const userService = fp(async (fastify, options) => {
|
|
|
99
99
|
};
|
|
100
100
|
|
|
101
101
|
const saveUser = async ({ id, ...otherInfo }) => {
|
|
102
|
-
const user = await getUserInstance(id);
|
|
102
|
+
const user = await getUserInstance({id});
|
|
103
103
|
|
|
104
104
|
if ((await accountIsExists({ phone: otherInfo.phone, email: otherInfo.email }, user)) > 0) {
|
|
105
105
|
throw new Error('手机号或者邮箱都不能重复');
|