@kne/fastify-account 2.0.0-alpha.0 → 2.0.0-alpha.2

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
@@ -17,7 +17,10 @@ const user = fp(
17
17
  isTest: false,
18
18
  jwt: {
19
19
  secret: 'super-secret',
20
- expires: null
20
+ expires: null,
21
+ verify: {
22
+ extractToken: request => request.headers['x-user-token']
23
+ }
21
24
  },
22
25
  defaultPassword: 'Aa000000!',
23
26
  sendMessage: async () => {}
@@ -45,7 +48,12 @@ const user = fp(
45
48
  {
46
49
  user: async request => {
47
50
  const { services } = fastify[options.name];
48
- const info = await request.jwtVerify();
51
+ let info;
52
+ try {
53
+ info = await request.jwtVerify();
54
+ } catch (e) {
55
+ throw Unauthorized('身份认证失败');
56
+ }
49
57
  //这里判断失效时间
50
58
  if (options.jwt.expires && Date.now() - info.iat * 1000 > options.jwt.expires) {
51
59
  throw Unauthorized('身份认证超时');
@@ -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;
@@ -160,13 +160,13 @@ const accountService = fp(async (fastify, options) => {
160
160
  await passwordAuthentication({ accountId: user.userAccountId, password });
161
161
 
162
162
  return {
163
- token: fastify.jwt.sign({ payload: { id: user.uuid } }),
164
- user: Object.assign({}, user.get({ plain: true }), { id: user.uuid })
163
+ token: fastify.jwt.sign({ payload: { id: user.id } }),
164
+ user: Object.assign({}, user.get({ plain: true }), { id: user.id })
165
165
  };
166
166
  };
167
167
 
168
168
  const resetPassword = async ({ password, userId }) => {
169
- const user = await services.user.getUserInstance({ uuid: userId });
169
+ const user = await services.user.getUserInstance({ id: userId });
170
170
  const account = await models.userAccount.create(
171
171
  Object.assign({}, await passwordEncryption(password), {
172
172
  belongToUserId: user.id
@@ -11,13 +11,13 @@ const adminService = fp(async (fastify, options) => {
11
11
  })) > 0) {
12
12
  throw new Error('系统已经初始化完成,不能执行该操作');
13
13
  }
14
- const currentUser = await services.user.getUserInstance({uuid: user.id});
14
+ const currentUser = await services.user.getUserInstance({id: user.id});
15
15
  currentUser.isSuperAdmin = true;
16
16
  await currentUser.save();
17
17
  };
18
18
 
19
19
  const checkIsSuperAdmin = async (user) => {
20
- const currentUser = await services.user.getUserInstance({uuid: user.id});
20
+ const currentUser = await services.user.getUserInstance({id: user.id});
21
21
  return currentUser.isSuperAdmin === true;
22
22
  };
23
23
 
@@ -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('手机号或者邮箱都不能重复');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "用于用户注册登录认证.",
5
5
  "main": "index.js",
6
6
  "scripts": {