@jsb188/auth-web 1.1.10 → 1.1.11

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,24 +1,28 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
- import { isPhone, isEmail, passwordStrength } from '@jsb188/app/utils/string.ts';
2
+ import {
3
+ isPhone,
4
+ isEmail,
5
+ passwordStrength,
6
+ } from '@jsb188/app/utils/string.ts';
3
7
 
4
8
  /**
5
9
  * Types
6
10
  */
7
11
 
8
12
  interface SignInFormType {
9
- identifier?: string;
10
- password?: string;
13
+ identifier?: string;
14
+ password?: string;
11
15
  }
12
16
 
13
17
  interface SignUpFormType {
14
- email: string;
15
- password: string;
16
- passwordRepeat: string;
18
+ email: string;
19
+ password: string;
20
+ passwordRepeat: string;
17
21
  }
18
22
 
19
23
  interface SignInDataForSchema {
20
- hidePassword: boolean;
21
- lastIdentifier: string;
24
+ hidePassword: boolean;
25
+ lastIdentifier: string;
22
26
  }
23
27
 
24
28
  /**
@@ -26,106 +30,122 @@ interface SignInDataForSchema {
26
30
  */
27
31
 
28
32
  const MIN_LEN_VALUES = {
29
- password: 8,
30
- description: 750,
33
+ password: 8,
34
+ description: 750,
31
35
  };
32
36
 
33
37
  /**
34
38
  * Rules for each field
35
39
  */
36
40
 
37
- const RULES = function() {
38
- return {
39
- email: {
40
- for: 'email',
41
- rule: isEmail,
42
- error: (value: string) => ({
43
- message: i18n.t('error.msg_email_invalid', { value }),
44
- }),
45
- },
46
- password: {
47
- for: 'password',
48
- rule: (value: string) => passwordStrength(value, MIN_LEN_VALUES.password).valid,
49
- error: (value: string) => {
50
- const { isLongEnough, hasLowercase, hasUppercase, hasDigit, hasSpecial } = passwordStrength(value, MIN_LEN_VALUES.password);
51
- const errorMessages = [];
41
+ const RULES = (function () {
42
+ return {
43
+ email: {
44
+ for: 'email',
45
+ rule: isEmail,
46
+ error: (value: string) => ({
47
+ message: i18n.t('error.msg_email_invalid', { value }),
48
+ }),
49
+ },
50
+ password: {
51
+ for: 'password',
52
+ rule: (value: string) =>
53
+ passwordStrength(value, MIN_LEN_VALUES.password).valid,
54
+ error: (value: string) => {
55
+ const {
56
+ isLongEnough,
57
+ hasLowercase,
58
+ hasUppercase,
59
+ hasDigit,
60
+ hasSpecial,
61
+ } = passwordStrength(value, MIN_LEN_VALUES.password);
62
+ const errorMessages = [];
52
63
 
53
- if (!isLongEnough) {
54
- errorMessages.push(
55
- i18n.t('error.msg_password_too_short', { length: MIN_LEN_VALUES.password }),
56
- );
57
- }
58
- if (!hasLowercase) {
59
- errorMessages.push(i18n.t('error.msg_password_lowercase'));
60
- }
61
- if (!hasUppercase) {
62
- errorMessages.push(i18n.t('error.msg_password_uppercase'));
63
- }
64
- if (!hasDigit) {
65
- errorMessages.push(i18n.t('error.msg_password_digit'));
66
- }
67
- if (!hasSpecial) {
68
- errorMessages.push(i18n.t('error.msg_password_special'));
69
- }
64
+ if (!isLongEnough) {
65
+ errorMessages.push(
66
+ i18n.t('error.msg_password_too_short', {
67
+ length: MIN_LEN_VALUES.password,
68
+ }),
69
+ );
70
+ }
71
+ if (!hasLowercase) {
72
+ errorMessages.push(i18n.t('error.msg_password_lowercase'));
73
+ }
74
+ if (!hasUppercase) {
75
+ errorMessages.push(i18n.t('error.msg_password_uppercase'));
76
+ }
77
+ if (!hasDigit) {
78
+ errorMessages.push(i18n.t('error.msg_password_digit'));
79
+ }
80
+ if (!hasSpecial) {
81
+ errorMessages.push(i18n.t('error.msg_password_special'));
82
+ }
70
83
 
71
- return {
72
- message: errorMessages.length > 1 ? i18n.t('error.msg_password_insecure') : errorMessages[0],
73
- };
74
- },
75
- },
76
- passwordRepeat: {
77
- for: 'passwordRepeat',
78
- rule: (value: string, formValues: Partial<SignUpFormType>) => value === formValues.password,
79
- error: {
80
- message: i18n.t('error.msg_password_mismatch'),
81
- },
82
- },
83
- phone: {
84
- for: 'phone',
85
- rule: (value: string) => isPhone(value),
86
- error: (value: string) => ({
87
- message: i18n.t('error.msg_phone_invalid', { value }),
88
- }),
89
- }
90
- };
91
- }();
84
+ return {
85
+ message:
86
+ errorMessages.length > 1
87
+ ? i18n.t('error.msg_password_insecure')
88
+ : errorMessages[0],
89
+ };
90
+ },
91
+ },
92
+ passwordRepeat: {
93
+ for: 'passwordRepeat',
94
+ rule: (value: string, formValues: Partial<SignUpFormType>) =>
95
+ value === formValues.password,
96
+ error: {
97
+ message: i18n.t('error.msg_password_mismatch'),
98
+ },
99
+ },
100
+ phone: {
101
+ for: 'phone',
102
+ rule: (value: string) => isPhone(value),
103
+ error: (value: string) => ({
104
+ message: i18n.t('error.msg_phone_invalid', { value }),
105
+ }),
106
+ },
107
+ };
108
+ })();
92
109
 
93
110
  /**
94
111
  * Schema; Change password
95
112
  */
96
113
 
97
114
  export const makeChangeAccountPasswordSchema = () => ({
98
- listData: [{
99
- __type: 'input',
100
- hidden: (dataForSchema?: SignInDataForSchema) => !!dataForSchema?.hidePassword,
101
- item: {
102
- autoFocus: true,
103
- name: 'currentPassword',
104
- type: 'password',
105
- label: i18n.t('account.currentPassword'),
106
- placeholder: i18n.t('account.password_ph'),
107
- // info: 'whatever you want',
108
- },
109
- }, {
110
- __type: 'password',
111
- item: {
112
- name: 'password',
113
- type: 'password',
114
- label: i18n.t('account.newPassword'),
115
- // placeholder: ''
116
- // info: 'whatever you want',
117
- },
118
- }, {
119
- __type: 'password',
120
- item: {
121
- name: 'passwordRepeat',
122
- type: 'password',
123
- label: i18n.t('account.newPasswordRepeat'),
124
- },
125
- }],
126
- rules: [
127
- RULES.password,
128
- ],
115
+ listData: [
116
+ {
117
+ __type: 'input',
118
+ hidden: (dataForSchema?: SignInDataForSchema) =>
119
+ !!dataForSchema?.hidePassword,
120
+ item: {
121
+ autoFocus: true,
122
+ name: 'currentPassword',
123
+ type: 'password',
124
+ label: i18n.t('account.currentPassword'),
125
+ placeholder: i18n.t('account.password_ph'),
126
+ // info: 'whatever you want',
127
+ },
128
+ },
129
+ {
130
+ __type: 'password',
131
+ item: {
132
+ name: 'password',
133
+ type: 'password',
134
+ label: i18n.t('account.newPassword'),
135
+ // placeholder: ''
136
+ // info: 'whatever you want',
137
+ },
138
+ },
139
+ {
140
+ __type: 'password',
141
+ item: {
142
+ name: 'passwordRepeat',
143
+ type: 'password',
144
+ label: i18n.t('account.newPasswordRepeat'),
145
+ },
146
+ },
147
+ ],
148
+ rules: [RULES.password],
129
149
  });
130
150
 
131
151
  /**
@@ -133,27 +153,28 @@ export const makeChangeAccountPasswordSchema = () => ({
133
153
  */
134
154
 
135
155
  export const makeAssignPasswordSchema = () => ({
136
- listData: [{
137
- __type: 'password',
138
- item: {
139
- name: 'password',
140
- type: 'password',
141
- label: i18n.t('account.password'),
142
- },
143
- }, {
144
- __type: 'password',
145
- item: {
146
- name: 'passwordRepeat',
147
- type: 'password',
148
- label: i18n.t('account.passwordRepeat'),
149
- },
150
- }],
151
- rules: [
152
- RULES.password,
153
- ],
154
- isButtonDisabled: (formValues: any) => {
155
- return !formValues?.password?.trim() || !formValues?.passwordRepeat?.trim();
156
- },
156
+ listData: [
157
+ {
158
+ __type: 'password',
159
+ item: {
160
+ name: 'password',
161
+ type: 'password',
162
+ label: i18n.t('account.password'),
163
+ },
164
+ },
165
+ {
166
+ __type: 'password',
167
+ item: {
168
+ name: 'passwordRepeat',
169
+ type: 'password',
170
+ label: i18n.t('account.passwordRepeat'),
171
+ },
172
+ },
173
+ ],
174
+ rules: [RULES.password],
175
+ isButtonDisabled: (formValues: any) => {
176
+ return !formValues?.password?.trim() || !formValues?.passwordRepeat?.trim();
177
+ },
157
178
  });
158
179
 
159
180
  /**
@@ -161,30 +182,34 @@ export const makeAssignPasswordSchema = () => ({
161
182
  */
162
183
 
163
184
  export const makeSignInSchema = () => ({
164
- listData: [{
165
- __type: 'input',
166
- // Can't do this because disabling the input will make post form ignore this field
167
- // locked: ({ hidePassword, lastIdentifier }: SignInDataForSchema) => !hidePassword && !!lastIdentifier,
168
- item: {
169
- name: 'identifier',
170
- allowClearIfLocked: true,
171
- label: i18n.t('auth.login_opts_main'),
172
- placeholder: i18n.t('account.email_ph'),
173
- },
174
- }, {
175
- __type: 'password',
176
- hidden: (dataForSchema?: SignInDataForSchema) => !!dataForSchema?.hidePassword,
177
- autoFocus: true,
178
- item: {
179
- name: 'password',
180
- type: 'password',
181
- label: i18n.t('account.password'),
182
- placeholder: i18n.t('account.password_ph'),
183
- },
184
- }],
185
- isButtonDisabled: (formValues: SignInFormType) => {
186
- return !formValues?.identifier?.trim();
187
- },
185
+ listData: [
186
+ {
187
+ __type: 'input',
188
+ // Can't do this because disabling the input will make post form ignore this field
189
+ // locked: ({ hidePassword, lastIdentifier }: SignInDataForSchema) => !hidePassword && !!lastIdentifier,
190
+ item: {
191
+ name: 'identifier',
192
+ allowClearIfLocked: true,
193
+ label: i18n.t('auth.login_opts_main'),
194
+ placeholder: i18n.t('account.email_ph'),
195
+ },
196
+ },
197
+ {
198
+ __type: 'password',
199
+ hidden: (dataForSchema?: SignInDataForSchema) =>
200
+ !!dataForSchema?.hidePassword,
201
+ autoFocus: true,
202
+ item: {
203
+ name: 'password',
204
+ type: 'password',
205
+ label: i18n.t('account.password'),
206
+ placeholder: i18n.t('account.password_ph'),
207
+ },
208
+ },
209
+ ],
210
+ isButtonDisabled: (formValues: SignInFormType) => {
211
+ return !formValues?.identifier?.trim();
212
+ },
188
213
  });
189
214
 
190
215
  /**
@@ -192,39 +217,43 @@ export const makeSignInSchema = () => ({
192
217
  */
193
218
 
194
219
  export const makeSignUpSchema = () => ({
195
- listData: [{
196
- __type: 'input',
197
- item: {
198
- name: 'email',
199
- label: i18n.t('account.email'),
200
- placeholder: i18n.t('account.email_ph'),
201
- },
202
- }, {
203
- __type: 'password',
204
- item: {
205
- name: 'password',
206
- type: 'password',
207
- label: i18n.t('account.password'),
208
- placeholder: i18n.t('account.password_ph'),
209
- },
210
- }, {
211
- __type: 'password',
212
- item: {
213
- name: 'passwordRepeat',
214
- type: 'password',
215
- label: i18n.t('account.passwordRepeat'),
216
- placeholder: i18n.t('account.password_ph'),
217
- },
218
- }],
219
- isButtonDisabled: (formValues: Partial<SignUpFormType>) => {
220
- const fields: (keyof SignUpFormType)[] = ['email', 'password', 'passwordRepeat'];
221
- return fields.some((s) => !formValues[s]?.trim());
222
- },
223
- rules: [
224
- RULES.email,
225
- RULES.password,
226
- RULES.passwordRepeat,
227
- ],
220
+ listData: [
221
+ {
222
+ __type: 'input',
223
+ item: {
224
+ name: 'email',
225
+ label: i18n.t('account.email'),
226
+ placeholder: i18n.t('account.email_ph'),
227
+ },
228
+ },
229
+ {
230
+ __type: 'password',
231
+ item: {
232
+ name: 'password',
233
+ type: 'password',
234
+ label: i18n.t('account.password'),
235
+ placeholder: i18n.t('account.password_ph'),
236
+ },
237
+ },
238
+ {
239
+ __type: 'password',
240
+ item: {
241
+ name: 'passwordRepeat',
242
+ type: 'password',
243
+ label: i18n.t('account.passwordRepeat'),
244
+ placeholder: i18n.t('account.password_ph'),
245
+ },
246
+ },
247
+ ],
248
+ isButtonDisabled: (formValues: Partial<SignUpFormType>) => {
249
+ const fields: (keyof SignUpFormType)[] = [
250
+ 'email',
251
+ 'password',
252
+ 'passwordRepeat',
253
+ ];
254
+ return fields.some((s) => !formValues[s]?.trim());
255
+ },
256
+ rules: [RULES.email, RULES.password, RULES.passwordRepeat],
228
257
  });
229
258
 
230
259
  /**
@@ -232,24 +261,24 @@ export const makeSignUpSchema = () => ({
232
261
  */
233
262
 
234
263
  interface RequestTokenizedEmailObj {
235
- email: string;
264
+ email: string;
236
265
  }
237
266
 
238
267
  export const makeRequestTokenizedEmailSchema = () => ({
239
- listData: [{
240
- __type: 'input',
241
- item: {
242
- name: 'email',
243
- label: i18n.t('account.email'),
244
- placeholder: i18n.t('account.email_ph'),
245
- },
246
- }],
247
- isButtonDisabled: (formValues: Partial<RequestTokenizedEmailObj>) => {
248
- return !formValues.email?.trim();
249
- },
250
- rules: [
251
- RULES.email,
252
- ],
268
+ listData: [
269
+ {
270
+ __type: 'input',
271
+ item: {
272
+ name: 'email',
273
+ label: i18n.t('account.email'),
274
+ placeholder: i18n.t('account.email_ph'),
275
+ },
276
+ },
277
+ ],
278
+ isButtonDisabled: (formValues: Partial<RequestTokenizedEmailObj>) => {
279
+ return !formValues.email?.trim();
280
+ },
281
+ rules: [RULES.email],
253
282
  });
254
283
 
255
284
  /**
@@ -257,22 +286,22 @@ export const makeRequestTokenizedEmailSchema = () => ({
257
286
  */
258
287
 
259
288
  interface SendPhoneVerificationObj {
260
- phone: string;
289
+ phone: string;
261
290
  }
262
291
 
263
292
  export const makeSendPhoneVerificationSchema = () => ({
264
- listData: [{
265
- __type: 'input',
266
- item: {
267
- name: 'phone',
268
- label: i18n.t('form.phone_number'),
269
- placeholder: i18n.t('account.phone_ph'),
270
- },
271
- }],
272
- isButtonDisabled: (formValues: Partial<SendPhoneVerificationObj>) => {
273
- return !formValues.phone?.trim();
274
- },
275
- rules: [
276
- RULES.phone,
277
- ],
293
+ listData: [
294
+ {
295
+ __type: 'input',
296
+ item: {
297
+ name: 'phone',
298
+ label: i18n.t('form.phone_number'),
299
+ placeholder: i18n.t('account.phone_ph'),
300
+ },
301
+ },
302
+ ],
303
+ isButtonDisabled: (formValues: Partial<SendPhoneVerificationObj>) => {
304
+ return !formValues.phone?.trim();
305
+ },
306
+ rules: [RULES.phone],
278
307
  });