@kne/fastify-account 2.0.0-alpha.5 → 2.0.0

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.
@@ -7,7 +7,8 @@ const accountController = fp(async (fastify, options) => {
7
7
  tags: ['账号'], summary: '发送登录邮箱验证码', body: {
8
8
  type: 'object', required: ['email'], properties: {
9
9
  email: { type: 'string', description: '邮箱' },
10
- type: { type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码', default: 0 }
10
+ type: { type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码', default: 0 },
11
+ options: { type: 'object' }
11
12
  }
12
13
  }, response: {
13
14
  200: {
@@ -24,8 +25,8 @@ const accountController = fp(async (fastify, options) => {
24
25
  }
25
26
  }
26
27
  }, async request => {
27
- const { email, type } = request.body;
28
- const code = await services.account.sendVerificationCode({ name: email, type });
28
+ const { email, type, options: otherOptions } = request.body;
29
+ const code = await services.account.sendVerificationCode({ name: email, type, options: otherOptions });
29
30
  return options.isTest ? { code } : {};
30
31
  });
31
32
 
@@ -33,7 +34,9 @@ const accountController = fp(async (fastify, options) => {
33
34
  schema: {
34
35
  tags: ['账号'], summary: '发送登录短信验证码', body: {
35
36
  type: 'object', required: ['phone'], properties: {
36
- phone: { type: 'string', description: '电话' }
37
+ phone: { type: 'string', description: '电话' },
38
+ type: { type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码', default: 0 },
39
+ options: { type: 'object' }
37
40
  }
38
41
  }, response: {
39
42
  200: {
@@ -50,8 +53,8 @@ const accountController = fp(async (fastify, options) => {
50
53
  }
51
54
  }
52
55
  }, async request => {
53
- const { phone } = request.body;
54
- const code = await services.account.sendVerificationCode({ name: phone, type: 0 });
56
+ const { phone, type, options: otherOptions } = request.body;
57
+ const code = await services.account.sendVerificationCode({ name: phone, type, options: otherOptions });
55
58
  return options.isTest ? { code } : {};
56
59
  });
57
60
 
@@ -24,10 +24,7 @@ const accountService = fp(async (fastify, options) => {
24
24
  const verificationCodeValidate = async ({ name, type, code }) => {
25
25
  const verificationCode = await models.verificationCode.findOne({
26
26
  where: {
27
- name,
28
- type,
29
- code,
30
- status: {
27
+ name, type, code, status: {
31
28
  [fastify.sequelize.Sequelize.Op.or]: [0, 1]
32
29
  }
33
30
  }
@@ -44,41 +41,34 @@ const accountService = fp(async (fastify, options) => {
44
41
 
45
42
  const generateVerificationCode = async ({ name, type }) => {
46
43
  const code = generateRandom6DigitNumber();
47
- await models.verificationCode.update(
48
- {
49
- status: 2
50
- },
51
- {
52
- where: {
53
- name,
54
- type,
55
- status: 0
56
- }
44
+ await models.verificationCode.update({
45
+ status: 2
46
+ }, {
47
+ where: {
48
+ name, type, status: 0
57
49
  }
58
- );
50
+ });
59
51
  await models.verificationCode.create({
60
- name,
61
- type,
62
- code
52
+ name, type, code
63
53
  });
64
54
  return code;
65
55
  };
66
56
 
67
- const sendVerificationCode = async ({ name, type }) => {
57
+ const sendVerificationCode = async ({ name, type, options: otherOptions }) => {
68
58
  // messageType: 0:短信验证码,1:邮件验证码 type: 0:注册,2:登录,4:验证租户管理员,5:忘记密码
69
59
  const code = await generateVerificationCode({ name, type });
70
60
  const isEmail = userNameIsEmail(name);
71
61
  // 这里写发送逻辑
72
- await options.sendMessage({ name, type, messageType: isEmail ? 1 : 0, props: { code } });
62
+ await options.sendMessage({ name, type, messageType: isEmail ? 1 : 0, props: { code, options: otherOptions } });
73
63
  return code;
74
64
  };
75
65
 
76
- const sendJWTVerificationCode = async ({ name, type }) => {
66
+ const sendJWTVerificationCode = async ({ name, type, options: otherOptions }) => {
77
67
  const code = await generateVerificationCode({ name, type });
78
68
  const token = fastify.jwt.sign({ name, type, code });
79
69
  const isEmail = userNameIsEmail(name);
80
70
  // 这里写发送逻辑
81
- await options.sendMessage({ name, type, messageType: isEmail ? 1 : 0, props: { token } });
71
+ await options.sendMessage({ name, type, messageType: isEmail ? 1 : 0, props: { token, options: otherOptions } });
82
72
  return token;
83
73
  };
84
74
 
@@ -96,8 +86,7 @@ const accountService = fp(async (fastify, options) => {
96
86
  const hash = await bcrypt.hash(combinedString, salt);
97
87
 
98
88
  return {
99
- password: hash,
100
- salt
89
+ password: hash, salt
101
90
  };
102
91
  };
103
92
 
@@ -112,22 +101,16 @@ const accountService = fp(async (fastify, options) => {
112
101
  }
113
102
  };
114
103
 
115
- const register = async ({ avatar, nickname, gender, birthday, description, phone, email, code, password, status }) => {
104
+ const register = async ({
105
+ avatar, nickname, gender, birthday, description, phone, email, code, password, status
106
+ }) => {
116
107
  const type = phone ? 0 : 1;
117
108
  if (!(await verificationCodeValidate({ name: type === 0 ? phone : email, type: 0, code }))) {
118
109
  throw new Error('验证码不正确或者已经过期');
119
110
  }
120
111
 
121
112
  return await services.user.addUser({
122
- avatar,
123
- nickname,
124
- gender,
125
- birthday,
126
- description,
127
- phone,
128
- email,
129
- password,
130
- status
113
+ avatar, nickname, gender, birthday, description, phone, email, password, status
131
114
  });
132
115
  };
133
116
 
@@ -167,11 +150,9 @@ const accountService = fp(async (fastify, options) => {
167
150
 
168
151
  const resetPassword = async ({ password, userId }) => {
169
152
  const user = await services.user.getUserInstance({ id: userId });
170
- const account = await models.userAccount.create(
171
- Object.assign({}, await passwordEncryption(password), {
172
- belongToUserId: user.id
173
- })
174
- );
153
+ const account = await models.userAccount.create(Object.assign({}, await passwordEncryption(password), {
154
+ belongToUserId: user.id
155
+ }));
175
156
  await user.update({ userAccountId: account.id });
176
157
  };
177
158
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "2.0.0-alpha.5",
3
+ "version": "2.0.0",
4
4
  "description": "用于用户注册登录认证.",
5
5
  "main": "index.js",
6
6
  "scripts": {