@jsb188/auth-web 1.1.10 → 1.1.12

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,278 +1,177 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
- import { isPhone, isEmail, passwordStrength } from '@jsb188/app/utils/string.ts';
2
+ import { defineForm } from '@jsb188/form/libs/defineForm';
3
+ import { rules } from '@jsb188/form/libs/rules';
4
+ import { z } from 'zod';
3
5
 
4
- /**
5
- * Types
6
- */
7
-
8
- interface SignInFormType {
9
- identifier?: string;
10
- password?: string;
11
- }
6
+ export type SignInFormValues = z.input<ReturnType<typeof makeSignInSchema>['schema']>;
12
7
 
13
- interface SignUpFormType {
14
- email: string;
15
- password: string;
16
- passwordRepeat: string;
8
+ export interface SignInFormContext {
9
+ hidePassword: boolean;
10
+ lastIdentifier?: string;
17
11
  }
18
12
 
19
- interface SignInDataForSchema {
20
- hidePassword: boolean;
21
- lastIdentifier: string;
22
- }
23
-
24
- /**
25
- * Values for validation
26
- */
27
-
28
- const MIN_LEN_VALUES = {
29
- password: 8,
30
- description: 750,
31
- };
32
-
33
- /**
34
- * Rules for each field
35
- */
36
-
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 = [];
52
-
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
- }
70
-
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
- }();
92
-
93
- /**
94
- * Schema; Change password
13
+ /*
14
+ * Define the current password and matching new password fields for account changes and token resets.
95
15
  */
16
+ export function makeChangeAccountPasswordSchema() {
17
+ return defineForm({
18
+ schema: z
19
+ .object({
20
+ currentPassword: z.string(),
21
+ password: rules.password(),
22
+ passwordRepeat: z.string(),
23
+ })
24
+ .superRefine(rules.matches('passwordRepeat', 'password')),
25
+ defaults: { currentPassword: '', password: '', passwordRepeat: '' },
26
+ fields: {
27
+ currentPassword: {
28
+ kind: 'input',
29
+ type: 'password',
30
+ autoFocus: true,
31
+ label: i18n.t('account.currentPassword'),
32
+ placeholder: i18n.t('account.password_ph'),
33
+ visibleWhen: ({ context }: { context?: Partial<SignInFormContext> }) => !context?.hidePassword,
34
+ },
35
+ password: {
36
+ kind: 'password',
37
+ type: 'password',
38
+ label: i18n.t('account.newPassword'),
39
+ },
40
+ passwordRepeat: {
41
+ kind: 'password',
42
+ type: 'password',
43
+ label: i18n.t('account.newPasswordRepeat'),
44
+ },
45
+ },
46
+ });
47
+ }
96
48
 
97
- 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
- ],
129
- });
130
-
131
- /**
132
- * Schema; Assign a new password
49
+ /*
50
+ * Define a matching password pair when an account does not yet have a password.
133
51
  */
52
+ export function makeAssignPasswordSchema() {
53
+ return defineForm({
54
+ schema: z
55
+ .object({
56
+ password: rules.password(),
57
+ passwordRepeat: z.string(),
58
+ })
59
+ .superRefine(rules.matches('passwordRepeat', 'password')),
60
+ defaults: { password: '', passwordRepeat: '' },
61
+ fields: {
62
+ password: {
63
+ kind: 'password',
64
+ type: 'password',
65
+ label: i18n.t('account.password'),
66
+ },
67
+ passwordRepeat: {
68
+ kind: 'password',
69
+ type: 'password',
70
+ label: i18n.t('account.passwordRepeat'),
71
+ },
72
+ },
73
+ canSubmit: ({ values }) => !!values.password.trim() && !!values.passwordRepeat.trim(),
74
+ });
75
+ }
134
76
 
135
- 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
- },
157
- });
158
-
159
- /**
160
- * Schema; sign in
77
+ /*
78
+ * Define identifier discovery and the password step without applying new-password rules to existing passwords.
161
79
  */
80
+ export function makeSignInSchema() {
81
+ return defineForm({
82
+ schema: z.object({ identifier: z.string(), password: z.string() }),
83
+ defaults: { identifier: '', password: '' },
84
+ fields: {
85
+ identifier: {
86
+ kind: 'input',
87
+ // Keep the identifier enabled so native form POST includes it.
88
+ suppressHydrationWarning: true,
89
+ allowClearIfLocked: true,
90
+ label: i18n.t('auth.login_opts_main'),
91
+ placeholder: i18n.t('account.email_ph'),
92
+ },
93
+ password: {
94
+ kind: 'password',
95
+ type: 'password',
96
+ autoFocus: true,
97
+ suppressHydrationWarning: true,
98
+ label: i18n.t('account.password'),
99
+ placeholder: i18n.t('account.password_ph'),
100
+ visibleWhen: ({ context }: { context?: Partial<SignInFormContext> }) => !context?.hidePassword,
101
+ },
102
+ },
103
+ canSubmit: ({ values }) => !!values.identifier.trim(),
104
+ });
105
+ }
162
106
 
163
- 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
- },
188
- });
189
-
190
- /**
191
- * Schema; Sign up
107
+ /*
108
+ * Define the email sign-up fields and the shared password strength and confirmation rules.
192
109
  */
110
+ export function makeSignUpSchema() {
111
+ return defineForm({
112
+ schema: z
113
+ .object({
114
+ email: rules.email(),
115
+ password: rules.password(),
116
+ passwordRepeat: z.string(),
117
+ })
118
+ .superRefine(rules.matches('passwordRepeat', 'password')),
119
+ defaults: { email: '', password: '', passwordRepeat: '' },
120
+ fields: {
121
+ email: {
122
+ kind: 'input',
123
+ label: i18n.t('account.email'),
124
+ placeholder: i18n.t('account.email_ph'),
125
+ },
126
+ password: {
127
+ kind: 'password',
128
+ type: 'password',
129
+ label: i18n.t('account.password'),
130
+ placeholder: i18n.t('account.password_ph'),
131
+ },
132
+ passwordRepeat: {
133
+ kind: 'password',
134
+ type: 'password',
135
+ label: i18n.t('account.passwordRepeat'),
136
+ placeholder: i18n.t('account.password_ph'),
137
+ },
138
+ },
139
+ canSubmit: ({ values }) => !!values.email.trim() && !!values.password.trim() && !!values.passwordRepeat.trim(),
140
+ });
141
+ }
193
142
 
194
- 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
- ],
228
- });
229
-
230
- /**
231
- * Schema; Request a link to reset password
143
+ /*
144
+ * Define the email field shared by verification, recovery, and organization sign-in discovery.
232
145
  */
233
-
234
- interface RequestTokenizedEmailObj {
235
- email: string;
146
+ export function makeRequestTokenizedEmailSchema() {
147
+ return defineForm({
148
+ schema: z.object({ email: rules.email() }),
149
+ defaults: { email: '' },
150
+ fields: {
151
+ email: {
152
+ kind: 'input',
153
+ label: i18n.t('account.email'),
154
+ placeholder: i18n.t('account.email_ph'),
155
+ },
156
+ },
157
+ canSubmit: ({ values }) => !!values.email.trim(),
158
+ });
236
159
  }
237
160
 
238
- 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
- ],
253
- });
254
-
255
- /**
256
- * Schema; Send phone verification code
161
+ /*
162
+ * Define the phone field used to request a sign-in verification code.
257
163
  */
258
-
259
- interface SendPhoneVerificationObj {
260
- phone: string;
164
+ export function makeSendPhoneVerificationSchema() {
165
+ return defineForm({
166
+ schema: z.object({ phone: rules.phone() }),
167
+ defaults: { phone: '' },
168
+ fields: {
169
+ phone: {
170
+ kind: 'input',
171
+ label: i18n.t('c.phone_number'),
172
+ placeholder: i18n.t('account.phone_ph'),
173
+ },
174
+ },
175
+ canSubmit: ({ values }) => !!values.phone.trim(),
176
+ });
261
177
  }
262
-
263
- 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
- ],
278
- });