@kne/fastify-account 2.0.0-alpha.3 → 2.0.0-alpha.4

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.
@@ -1,263 +1,264 @@
1
1
  const fp = require('fastify-plugin');
2
2
 
3
3
  const accountController = fp(async (fastify, options) => {
4
- const {services} = fastify[options.name];
5
- fastify.post(`${options.prefix}/account/sendEmailCode`, {
6
- schema: {
7
- tags: ['账号'], summary: '发送登录邮箱验证码', body: {
8
- type: 'object', required: ['email'], properties: {
9
- email: {type: 'string', description: '邮箱'}
10
- }
11
- }, response: {
12
- 200: {
13
- content: {
14
- 'application/json': {
15
- schema: {
16
- type: 'object', properties: {
17
- code: {type: 'string', description: '验证码'}
18
- }
19
- }
20
- }
21
- }
22
- }
23
- }
4
+ const { services } = fastify[options.name];
5
+ fastify.post(`${options.prefix}/account/sendEmailCode`, {
6
+ schema: {
7
+ tags: ['账号'], summary: '发送登录邮箱验证码', body: {
8
+ type: 'object', required: ['email'], properties: {
9
+ email: { type: 'string', description: '邮箱' },
10
+ type: { type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码', default: 0 }
24
11
  }
25
- }, async request => {
26
- const {email} = request.body;
27
- const code = await services.account.sendVerificationCode({name: email, type: 0});
28
- return options.isTest ? {code} : {};
29
- });
30
-
31
- fastify.post(`${options.prefix}/account/sendSMSCode`, {
32
- schema: {
33
- tags: ['账号'], summary: '发送登录短信验证码', body: {
34
- type: 'object', required: ['phone'], properties: {
35
- phone: {type: 'string', description: '电话'}
36
- }
37
- }, response: {
38
- 200: {
39
- content: {
40
- 'application/json': {
41
- schema: {
42
- type: 'object', properties: {
43
- code: {type: 'string', description: '验证码'}
44
- }
45
- }
46
- }
47
- }
12
+ }, response: {
13
+ 200: {
14
+ content: {
15
+ 'application/json': {
16
+ schema: {
17
+ type: 'object', properties: {
18
+ code: { type: 'string', description: '验证码' }
48
19
  }
20
+ }
49
21
  }
22
+ }
50
23
  }
51
- }, async request => {
52
- const {phone} = request.body;
53
- const code = await services.account.sendVerificationCode({name: phone, type: 0});
54
- return options.isTest ? {code} : {};
55
- });
24
+ }
25
+ }
26
+ }, async request => {
27
+ const { email, type } = request.body;
28
+ const code = await services.account.sendVerificationCode({ name: email, type });
29
+ return options.isTest ? { code } : {};
30
+ });
56
31
 
57
- fastify.post(`${options.prefix}/account/validateCode`, {
58
- schema: {
59
- tags: ['账号'], summary: '验证码验证', body: {
60
- type: 'object', required: ['name', 'type', 'code'], properties: {
61
- name: {type: 'string', description: '被验证的账号,手机或邮箱'},
62
- type: {type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码'},
63
- code: {type: 'string', description: '接受到的验证码'}
64
- }
65
- }, response: {
66
- 200: {
67
- content: {
68
- 'application/json': {
69
- schema: {}
70
- }
71
- }
72
- }
73
- }
74
- }
75
- }, async request => {
76
- const {name, type, code} = request.body;
77
- const isPass = await services.account.verificationCodeValidate({
78
- name, type, code
79
- });
80
- if (!isPass) {
81
- throw new Error('验证码错误');
32
+ fastify.post(`${options.prefix}/account/sendSMSCode`, {
33
+ schema: {
34
+ tags: ['账号'], summary: '发送登录短信验证码', body: {
35
+ type: 'object', required: ['phone'], properties: {
36
+ phone: { type: 'string', description: '电话' }
82
37
  }
83
- return {};
84
- });
85
-
86
- fastify.post(`${options.prefix}/account/accountIsExists`, {
87
- schema: {
88
- tags: ['账号'], summary: '账号是否已存在', body: {
89
- oneOf: [{
90
- type: 'object', required: ['phone'], properties: {
91
- phone: {type: 'string', description: '电话'}
92
- }
93
- }, {
94
- type: 'object', required: ['email'], properties: {
95
- email: {type: 'string', description: '邮箱'}
96
- }
97
- }]
98
- }, response: {
99
- 200: {
100
- content: {
101
- 'application/json': {
102
- schema: {
103
- type: 'object', properties: {
104
- isExists: {type: 'boolean', description: 'true已存在,false不存在'}
105
- }
106
- }
107
- }
108
- }
38
+ }, response: {
39
+ 200: {
40
+ content: {
41
+ 'application/json': {
42
+ schema: {
43
+ type: 'object', properties: {
44
+ code: { type: 'string', description: '验证码' }
109
45
  }
46
+ }
110
47
  }
48
+ }
111
49
  }
112
- }, async request => {
113
- const {phone, email} = request.body;
114
- return {isExists: await services.user.accountIsExists({phone, email})};
115
- });
50
+ }
51
+ }
52
+ }, async request => {
53
+ const { phone } = request.body;
54
+ const code = await services.account.sendVerificationCode({ name: phone, type: 0 });
55
+ return options.isTest ? { code } : {};
56
+ });
116
57
 
117
- fastify.post(`${options.prefix}/account/register`, {
118
- schema: {
119
- tags: ['账号'], summary: '注册账号', body: {
120
- oneOf: [{
121
- type: 'object', required: ['phone', 'password', 'code'], properties: {
122
- avatar: {type: 'string', description: '头像图片id'},
123
- phone: {type: 'string', description: '电话'},
124
- code: {type: 'string', description: '验证码'},
125
- password: {type: 'string', description: '密码(需要md5加密)'},
126
- invitationCode: {type: 'string', description: '邀请码,用来默认加入租户'},
127
- nickname: {type: 'string', description: '昵称'},
128
- gender: {type: 'string', description: '性别'},
129
- birthday: {type: 'string', format: 'date', description: '出生日期'},
130
- description: {type: 'string', description: '个人简介'}
131
- }
132
- }, {
133
- type: 'object', required: ['email', 'password', 'code'], properties: {
134
- avatar: {type: 'string', description: '头像图片id'},
135
- email: {type: 'string', description: '邮箱'},
136
- code: {type: 'string', description: '验证码'},
137
- password: {type: 'string', description: '密码(需要md5加密)'},
138
- invitationCode: {type: 'string', description: '邀请码,用来默认加入租户'},
139
- nickname: {type: 'string', description: '昵称'},
140
- gender: {type: 'string', description: '性别'},
141
- birthday: {type: 'string', format: 'date', description: '出生日期'},
142
- description: {type: 'string', description: '个人简介'}
143
- }
144
- }]
145
- }, response: {
146
- 200: {
147
- content: {
148
- 'application/json': {
149
- schema: {}
150
- }
151
- }
152
- }
58
+ fastify.post(`${options.prefix}/account/validateCode`, {
59
+ schema: {
60
+ tags: ['账号'], summary: '验证码验证', body: {
61
+ type: 'object', required: ['name', 'type', 'code'], properties: {
62
+ name: { type: 'string', description: '被验证的账号,手机或邮箱' },
63
+ type: { type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码' },
64
+ code: { type: 'string', description: '接受到的验证码' }
65
+ }
66
+ }, response: {
67
+ 200: {
68
+ content: {
69
+ 'application/json': {
70
+ schema: {}
153
71
  }
72
+ }
154
73
  }
155
- }, async request => {
156
- const account = request.body;
157
- return await services.account.register(account);
74
+ }
75
+ }
76
+ }, async request => {
77
+ const { name, type, code } = request.body;
78
+ const isPass = await services.account.verificationCodeValidate({
79
+ name, type, code
158
80
  });
81
+ if (!isPass) {
82
+ throw new Error('验证码错误');
83
+ }
84
+ return {};
85
+ });
159
86
 
160
- fastify.post(`${options.prefix}/account/login`, {
161
- schema: {
162
- tags: ['账号'], summary: '登录', body: {
163
- type: 'object', required: ['password'], properties: {
164
- type: {type: 'string', default: 'email', description: '登录类型'},
165
- email: {type: 'string', description: '邮箱'},
166
- phone: {type: 'string', description: '手机号'},
167
- password: {type: 'string', description: '密码'}
168
- }
169
- }, response: {
170
- 200: {
171
- content: {
172
- 'application/json': {
173
- schema: {
174
- type: 'object', properties: {
175
- token: {type: 'string', description: '用户token'},
176
- currentTenantId: {type: 'string', description: '当前租户id'}
177
- }
178
- }
179
- }
180
- }
87
+ fastify.post(`${options.prefix}/account/accountIsExists`, {
88
+ schema: {
89
+ tags: ['账号'], summary: '账号是否已存在', body: {
90
+ oneOf: [{
91
+ type: 'object', required: ['phone'], properties: {
92
+ phone: { type: 'string', description: '电话' }
93
+ }
94
+ }, {
95
+ type: 'object', required: ['email'], properties: {
96
+ email: { type: 'string', description: '邮箱' }
97
+ }
98
+ }]
99
+ }, response: {
100
+ 200: {
101
+ content: {
102
+ 'application/json': {
103
+ schema: {
104
+ type: 'object', properties: {
105
+ isExists: { type: 'boolean', description: 'true已存在,false不存在' }
181
106
  }
107
+ }
182
108
  }
109
+ }
183
110
  }
184
- }, async request => {
185
- const appName = request.headers['x-app-name'];
186
- const {token, user} = await services.account.login(Object.assign({}, request.body, {appName}));
187
- return {token, currentTenantId: user.currentTenantId};
188
- });
111
+ }
112
+ }
113
+ }, async request => {
114
+ const { phone, email } = request.body;
115
+ return { isExists: await services.user.accountIsExists({ phone, email }) };
116
+ });
189
117
 
190
- fastify.post(`${options.prefix}/account/modifyPassword`, {
191
- schema: {
192
- tags: ['账号'], summary: '新用户重置新密码', body: {
193
- oneOf: [{
194
- type: 'object', required: ['email', 'newPwd', 'oldPwd'], properties: {
195
- email: {type: 'string', description: '邮箱'},
196
- newPwd: {type: 'string', description: '新密码'},
197
- oldPwd: {type: 'string', description: '原密码'}
198
- }
199
- }, {
200
- type: 'object', required: ['phone', 'newPwd', 'oldPwd'], properties: {
201
- phone: {type: 'string', description: '手机号'},
202
- newPwd: {type: 'string', description: '新密码'},
203
- oldPwd: {type: 'string', description: '原密码'}
204
- }
205
- }]
118
+ fastify.post(`${options.prefix}/account/register`, {
119
+ schema: {
120
+ tags: ['账号'], summary: '注册账号', body: {
121
+ oneOf: [{
122
+ type: 'object', required: ['phone', 'password', 'code'], properties: {
123
+ avatar: { type: 'string', description: '头像图片id' },
124
+ phone: { type: 'string', description: '电话' },
125
+ code: { type: 'string', description: '验证码' },
126
+ password: { type: 'string', description: '密码(需要md5加密)' },
127
+ invitationCode: { type: 'string', description: '邀请码,用来默认加入租户' },
128
+ nickname: { type: 'string', description: '昵称' },
129
+ gender: { type: 'string', description: '性别' },
130
+ birthday: { type: 'string', format: 'date', description: '出生日期' },
131
+ description: { type: 'string', description: '个人简介' }
132
+ }
133
+ }, {
134
+ type: 'object', required: ['email', 'password', 'code'], properties: {
135
+ avatar: { type: 'string', description: '头像图片id' },
136
+ email: { type: 'string', description: '邮箱' },
137
+ code: { type: 'string', description: '验证码' },
138
+ password: { type: 'string', description: '密码(需要md5加密)' },
139
+ invitationCode: { type: 'string', description: '邀请码,用来默认加入租户' },
140
+ nickname: { type: 'string', description: '昵称' },
141
+ gender: { type: 'string', description: '性别' },
142
+ birthday: { type: 'string', format: 'date', description: '出生日期' },
143
+ description: { type: 'string', description: '个人简介' }
144
+ }
145
+ }]
146
+ }, response: {
147
+ 200: {
148
+ content: {
149
+ 'application/json': {
150
+ schema: {}
206
151
  }
152
+ }
207
153
  }
208
- }, async request => {
209
- await services.account.modifyPassword(request.body);
210
- return {};
211
- });
154
+ }
155
+ }
156
+ }, async request => {
157
+ const account = request.body;
158
+ return await services.account.register(account);
159
+ });
212
160
 
213
- fastify.post(`${options.prefix}/account/resetPassword`, {
214
- schema: {
215
- tags: ['账号'], summary: '用户重置密码', body: {
216
- type: 'object', required: ['newPwd', 'token'], properties: {
217
- newPwd: {type: 'string', description: '新密码'}, token: {type: 'string', description: '验证token'}
161
+ fastify.post(`${options.prefix}/account/login`, {
162
+ schema: {
163
+ tags: ['账号'], summary: '登录', body: {
164
+ type: 'object', required: ['password'], properties: {
165
+ type: { type: 'string', default: 'email', description: '登录类型' },
166
+ email: { type: 'string', description: '邮箱' },
167
+ phone: { type: 'string', description: '手机号' },
168
+ password: { type: 'string', description: '密码' }
169
+ }
170
+ }, response: {
171
+ 200: {
172
+ content: {
173
+ 'application/json': {
174
+ schema: {
175
+ type: 'object', properties: {
176
+ token: { type: 'string', description: '用户token' },
177
+ currentTenantId: { type: 'string', description: '当前租户id' }
218
178
  }
179
+ }
219
180
  }
181
+ }
220
182
  }
221
- }, async request => {
222
- await services.account.resetPasswordByToken({
223
- password: request.body.newPwd, token: request.body.token
224
- });
183
+ }
184
+ }
185
+ }, async request => {
186
+ const appName = request.headers['x-app-name'];
187
+ const { token, user } = await services.account.login(Object.assign({}, request.body, { appName }));
188
+ return { token, currentTenantId: user.currentTenantId };
189
+ });
225
190
 
226
- return {};
227
- });
191
+ fastify.post(`${options.prefix}/account/modifyPassword`, {
192
+ schema: {
193
+ tags: ['账号'], summary: '新用户重置新密码', body: {
194
+ oneOf: [{
195
+ type: 'object', required: ['email', 'newPwd', 'oldPwd'], properties: {
196
+ email: { type: 'string', description: '邮箱' },
197
+ newPwd: { type: 'string', description: '新密码' },
198
+ oldPwd: { type: 'string', description: '原密码' }
199
+ }
200
+ }, {
201
+ type: 'object', required: ['phone', 'newPwd', 'oldPwd'], properties: {
202
+ phone: { type: 'string', description: '手机号' },
203
+ newPwd: { type: 'string', description: '新密码' },
204
+ oldPwd: { type: 'string', description: '原密码' }
205
+ }
206
+ }]
207
+ }
208
+ }
209
+ }, async request => {
210
+ await services.account.modifyPassword(request.body);
211
+ return {};
212
+ });
228
213
 
229
- fastify.post(`${options.prefix}/account/forgetPwd`, {
230
- schema: {
231
- tags: ['账号'], summary: '忘记密码', body: {
232
- oneOf: [{
233
- type: 'object', required: ['email'], properties: {
234
- email: {type: 'string', description: '邮箱'}
235
- }
236
- }, {
237
- type: 'object', required: ['phone'], properties: {
238
- phone: {type: 'string', description: '手机号'}
239
- }
240
- }]
241
- }
214
+ fastify.post(`${options.prefix}/account/resetPassword`, {
215
+ schema: {
216
+ tags: ['账号'], summary: '用户重置密码', body: {
217
+ type: 'object', required: ['newPwd', 'token'], properties: {
218
+ newPwd: { type: 'string', description: '新密码' }, token: { type: 'string', description: '验证token' }
242
219
  }
243
- }, async request => {
244
- const name = request.body.email || request.body.phone;
245
- const token = await services.account.sendJWTVerificationCode({name, type: 5});
246
- return options.isTest ? {token} : {};
220
+ }
221
+ }
222
+ }, async request => {
223
+ await services.account.resetPasswordByToken({
224
+ password: request.body.newPwd, token: request.body.token
247
225
  });
248
226
 
249
- fastify.post(`${options.prefix}/account/parseResetToken`, {
250
- schema: {
251
- tags: ['账号'], summary: '通过token获取name', body: {
252
- type: 'object', required: ['token'], properties: {
253
- token: {type: 'string'}
254
- }
255
- }
227
+ return {};
228
+ });
229
+
230
+ fastify.post(`${options.prefix}/account/forgetPwd`, {
231
+ schema: {
232
+ tags: ['账号'], summary: '忘记密码', body: {
233
+ oneOf: [{
234
+ type: 'object', required: ['email'], properties: {
235
+ email: { type: 'string', description: '邮箱' }
236
+ }
237
+ }, {
238
+ type: 'object', required: ['phone'], properties: {
239
+ phone: { type: 'string', description: '手机号' }
240
+ }
241
+ }]
242
+ }
243
+ }
244
+ }, async request => {
245
+ const name = request.body.email || request.body.phone;
246
+ const token = await services.account.sendJWTVerificationCode({ name, type: 5 });
247
+ return options.isTest ? { token } : {};
248
+ });
249
+
250
+ fastify.post(`${options.prefix}/account/parseResetToken`, {
251
+ schema: {
252
+ tags: ['账号'], summary: '通过token获取name', body: {
253
+ type: 'object', required: ['token'], properties: {
254
+ token: { type: 'string' }
256
255
  }
257
- }, async request => {
258
- const {name} = await services.account.verificationJWTCodeValidate(request.body);
259
- return {name};
260
- });
256
+ }
257
+ }
258
+ }, async request => {
259
+ const { name } = await services.account.verificationJWTCodeValidate(request.body);
260
+ return { name };
261
+ });
261
262
  });
262
263
 
263
264
  module.exports = accountController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-account",
3
- "version": "2.0.0-alpha.3",
3
+ "version": "2.0.0-alpha.4",
4
4
  "description": "用于用户注册登录认证.",
5
5
  "main": "index.js",
6
6
  "scripts": {