@jsb188/auth-web 1.1.11 → 1.1.13

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,177 +1,114 @@
1
1
  import { SUPPORTED_LANGUAGES } from '@jsb188/app/constants/app.ts';
2
2
  import i18n from '@jsb188/app/i18n/index.ts';
3
- import type { AccountData } from '@jsb188/app/types/auth.d.ts';
3
+ import type { AccountData, AccountSettings } from '@jsb188/app/types/auth.d.ts';
4
4
  import { formatPhoneNumber } from '@jsb188/app/utils/string.ts';
5
+ import { defineForm } from '@jsb188/form/libs/defineForm';
5
6
  import type { OpenModalPopUpFn } from '@jsb188/react/states';
7
+ import { z } from 'zod';
6
8
 
7
- /**
8
- * Length values for validation
9
- */
10
-
11
- const MIN_LEN_VALUES = {
12
- password: 8,
13
- firstName: 32,
14
- lastName: 32,
15
- description: 750,
16
- email: 80,
17
- phone: 15,
18
- };
19
-
20
- /**
21
- * Schema; Edit account
22
- */
23
-
9
+ /* Define editable account values and the existing verification action controls. */
24
10
  export function makeEditAccountSchema(
25
- formId: string,
26
- focusedName: string,
27
- formValues: any,
28
11
  account: AccountData | null,
12
+ settings: AccountSettings | null,
29
13
  openModalPopUp: OpenModalPopUpFn,
30
14
  ) {
31
- const poProps = {
32
- // scrollAreaDOMId,
33
- zClassName: 'z9',
34
- position: 'bottom_left',
35
- offsetX: 0,
36
- offsetY: 7,
37
- };
38
-
39
- return {
40
- listData: [
41
- {
42
- // __type: 'subtitle',
43
- // item: {
44
- // text: i18n.t('account.profile'),
45
- // },
46
- // }, {
47
- __type: 'group',
48
- item: {
49
- items: [
50
- {
51
- __type: 'input',
52
- item: {
53
- name: 'profile.firstName',
54
- maxLength: MIN_LEN_VALUES.firstName,
55
- label: i18n.t('account.fName'),
56
- placeholder: account?.profile?.firstName,
57
- autoComplete: 'off',
58
- },
59
- },
60
- {
61
- __type: 'input',
62
- item: {
63
- name: 'profile.lastName',
64
- maxLength: MIN_LEN_VALUES.lastName,
65
- label: i18n.t('account.lName'),
66
- placeholder: account?.profile?.lastName,
67
- autoComplete: 'off',
68
- },
69
- },
70
- ],
71
- },
15
+ return defineForm({
16
+ schema: z.object({
17
+ profile: z.object({ firstName: z.string(), lastName: z.string() }),
18
+ settings: z.object({ language: z.string() }),
19
+ change_phoneNumber: z.string(),
20
+ change_emailAddress: z.string(),
21
+ change_password: z.string(),
22
+ }),
23
+ defaults: {
24
+ profile: { firstName: account?.profile?.firstName || '', lastName: account?.profile?.lastName || '' },
25
+ settings: { language: settings?.language || 'en' },
26
+ change_phoneNumber: '',
27
+ change_emailAddress: '',
28
+ change_password: '',
29
+ },
30
+ fields: {
31
+ 'profile.firstName': {
32
+ kind: 'input',
33
+ maxLength: 32,
34
+ label: i18n.t('account.fName'),
35
+ placeholder: account?.profile?.firstName,
36
+ autoComplete: 'off',
72
37
  },
73
- {
74
- __type: 'input_click',
75
- forceClickId: 'input_click_settings.language',
76
- label: i18n.t('form.activity'),
77
- item: {
78
- label: i18n.t('account.ai_language'),
79
- locked: true,
80
- focused: focusedName === formId + '_settings.language',
81
- name: 'settings.language',
82
- placeholder: i18n.t(`form.activity_ph`),
83
- getter: (value: string) => i18n.t(`form.lang.${value || 'en'}`),
84
-
38
+ 'profile.lastName': {
39
+ kind: 'input',
40
+ maxLength: 32,
41
+ label: i18n.t('account.lName'),
42
+ placeholder: account?.profile?.lastName,
43
+ autoComplete: 'off',
44
+ },
45
+ 'settings.language': {
46
+ kind: 'click',
47
+ label: i18n.t('account.ai_language'),
48
+ placeholder: i18n.t('c.activity_ph'),
49
+ getter: (value: string) => i18n.t(`c.lang.${value || 'en'}`),
50
+ resolve: ({ values }) => ({
85
51
  popOverProps: {
86
- id: formId + '_settings.language',
87
- ...poProps,
52
+ id: 'edit_account_settings.language',
53
+ zClassName: 'z9',
54
+ position: 'bottom_left',
55
+ offsetX: 0,
56
+ offsetY: 7,
88
57
  iface: {
89
58
  name: 'PO_LIST',
90
59
  variables: {
91
60
  designClassName: 'w_400',
92
61
  className: 'max_h_40vh',
93
- options: SUPPORTED_LANGUAGES.map((lang) => ({
62
+ options: SUPPORTED_LANGUAGES.map((language) => ({
94
63
  __type: 'LIST_ITEM' as const,
95
- text: i18n.t(`form.lang.${lang}`),
96
- // textClassName: 'ft_xs',
97
- // rightIconClassName: 'ft_xs mb_2',
64
+ text: i18n.t(`c.lang.${language}`),
98
65
  name: 'settings.language',
99
- value: lang,
100
- selected: (formValues?.settings?.language || 'en') === lang,
66
+ value: language,
67
+ selected: (values.settings.language || 'en') === language,
101
68
  })),
102
69
  },
103
70
  },
104
71
  },
105
- },
72
+ }),
106
73
  },
107
- {
108
- __type: 'input_w_button',
109
- item: {
110
- name: 'change_phoneNumber',
111
- maxLength: MIN_LEN_VALUES.phone,
112
- setter: (data: any) =>
113
- formatPhoneNumber(
114
- data?.change_phoneNumber || data?.phone?.number || '',
115
- ),
116
- label: i18n.t('form.phone_number'),
117
- autoComplete: 'off',
118
- disabled: true,
119
- buttonText: i18n.t('form.change'),
120
- onClickButton: () =>
121
- openModalPopUp({
122
- preset: 'CHANGE_PHONE',
123
- name: 'edit_account_phone',
124
- }),
125
- },
74
+ change_phoneNumber: {
75
+ kind: 'action',
76
+ maxLength: 15,
77
+ getter: () => formatPhoneNumber(account?.phone?.number || '') || '',
78
+ label: i18n.t('c.phone_number'),
79
+ autoComplete: 'off',
80
+ disabled: true,
81
+ buttonText: i18n.t('c.change'),
82
+ onClickButton: () => openModalPopUp({ preset: 'CHANGE_PHONE', name: 'edit_account_phone' }),
126
83
  },
127
- {
128
- __type: 'input_w_button',
129
- item: {
130
- name: 'change_emailAddress',
131
- maxLength: MIN_LEN_VALUES.email,
132
- setter: (data: any) =>
133
- data?.change_emailAddress || data?.email?.address,
134
- label: i18n.t('account.email'),
135
- autoComplete: 'off',
136
- disabled: true,
137
- buttonText: i18n.t('form.change'),
138
- onClickButton: () =>
139
- openModalPopUp({
140
- preset: 'CHANGE_EMAIL',
141
- name: 'edit_account_email',
142
- }),
143
- },
84
+ change_emailAddress: {
85
+ kind: 'action',
86
+ maxLength: 80,
87
+ getter: () => account?.email?.address || '',
88
+ label: i18n.t('account.email'),
89
+ autoComplete: 'off',
90
+ disabled: true,
91
+ buttonText: i18n.t('c.change'),
92
+ onClickButton: () => openModalPopUp({ preset: 'CHANGE_EMAIL', name: 'edit_account_email' }),
144
93
  },
145
- {
146
- __type: 'input_w_button',
147
- item: {
148
- name: 'change_password',
149
- minLength: MIN_LEN_VALUES.password,
150
- label: i18n.t('account.password'),
151
- setter: () => '....',
152
- autoComplete: 'off',
153
- type: 'password',
154
- disabled: true,
155
- buttonText: i18n.t('form.change'),
156
- onClickButton: () =>
157
- openModalPopUp({
158
- preset: 'CHANGE_PASSWORD',
159
- name: 'edit_account_password',
160
- }),
161
- },
162
- // __type: 'textarea',
163
- // item: {
164
- // name: 'description',
165
- // maxLength: MIN_LEN_VALUES.description,
166
- // setter: (data: any) => data?.profile?.description,
167
- // label: i18n.t('account.about'),
168
- // placeholder: i18n.t('account.about_ph'),
169
- // autoComplete: 'off',
170
- // },
94
+ change_password: {
95
+ kind: 'action',
96
+ minLength: 8,
97
+ getter: () => '....',
98
+ label: i18n.t('account.password'),
99
+ autoComplete: 'off',
100
+ type: 'password',
101
+ disabled: true,
102
+ buttonText: i18n.t('c.change'),
103
+ onClickButton: () => openModalPopUp({ preset: 'CHANGE_PASSWORD', name: 'edit_account_password' }),
171
104
  },
105
+ },
106
+ layout: [
107
+ { kind: 'row', fields: ['profile.firstName', 'profile.lastName'] },
108
+ 'settings.language',
109
+ 'change_phoneNumber',
110
+ 'change_emailAddress',
111
+ 'change_password',
172
112
  ],
173
- rules: [
174
- // RULES.password,
175
- ],
176
- };
113
+ });
177
114
  }
@@ -1,182 +1,139 @@
1
1
  import { SUPPORTED_COUNTRIES } from '@jsb188/app/constants/app.ts';
2
2
  import i18n from '@jsb188/app/i18n/index.ts';
3
+ import type { AddressObj } from '@jsb188/app/types/other.d.ts';
4
+ import { hasAddressDetails } from '@jsb188/app/utils/string.ts';
5
+ import { defineForm } from '@jsb188/form/libs/defineForm';
6
+ import { z } from 'zod';
3
7
 
4
- /**
5
- * Length values for address field validation.
6
- */
7
-
8
- const MIN_LEN_VALUES = {
9
- city: 100,
10
- state: 32,
11
- };
12
-
13
- /**
14
- * Build a required-address-field validator.
15
- */
16
-
17
- function makeAddressRequiredRule(fieldName: string, labelKey: string) {
18
- return {
19
- for: `address.${fieldName}`,
20
- rule: (_value: unknown, formValues: any) => {
21
- const address = formValues?.address || {};
22
- const hasAddressValueExceptCountry = [
23
- 'line1',
24
- 'line2',
25
- 'city',
26
- 'state',
27
- 'postalCode',
28
- ].some((key) => {
29
- return String(address?.[key] || '').trim().length > 0;
30
- });
8
+ /* Validate an optional address, requiring its core fields once an address is entered. */
9
+ export function makeAddressValueSchema() {
10
+ return z
11
+ .object({
12
+ line1: z.string(),
13
+ line2: z.string(),
14
+ city: z.string(),
15
+ state: z.string(),
16
+ postalCode: z.string(),
17
+ country: z.string().trim(),
18
+ })
19
+ .superRefine((address, context) => {
20
+ if (!hasAddressDetails(address)) {
21
+ return;
22
+ }
31
23
 
32
- if (!hasAddressValueExceptCountry) {
33
- return true;
24
+ if (address.country && !SUPPORTED_COUNTRIES.includes(address.country)) {
25
+ context.addIssue({ code: z.ZodIssueCode.custom, path: ['country'], message: i18n.t('error.20099') });
34
26
  }
35
27
 
36
- return String(address?.[fieldName] || '').trim().length > 0;
37
- },
38
- error: () => ({
39
- message: i18n.t('error.msg_value_required', { value: i18n.t(labelKey) }),
40
- }),
41
- };
28
+ for (const field of ['line1', 'postalCode', 'city', 'state', 'country'] as const) {
29
+ if (!address[field].trim()) {
30
+ context.addIssue({
31
+ code: z.ZodIssueCode.custom,
32
+ path: [field],
33
+ message: i18n.t('error.msg_value_required', { value: i18n.t(`c.${field}`) }),
34
+ });
35
+ }
36
+ }
37
+ });
42
38
  }
43
39
 
44
- /**
45
- * Create reusable address schema section and rules.
46
- */
47
-
48
- export function makeAddressSchema(
49
- formId: string,
50
- focusedName: string,
51
- addressFormValues?: Partial<{
52
- line1: string | null;
53
- line2: string | null;
54
- city: string | null;
55
- state: string | null;
56
- postalCode: string | null;
57
- country: string | null;
58
- }> | null,
59
- popOverDesignClassName?: string,
60
- ) {
61
- const currentAddress = addressFormValues || {};
62
- const selectedCountryCode = currentAddress?.country || '';
63
- const priorityCountryOptions = ['US', 'MX', 'CA'].map((countryCode) => ({
64
- __type: 'LIST_ITEM' as const,
65
- text: i18n.t(`country.from_code.${countryCode}`),
66
- name: 'address.country',
67
- value: countryCode,
68
- selected: selectedCountryCode === countryCode,
69
- }));
70
- const supportedCountryOptions = SUPPORTED_COUNTRIES.map((countryCode) => ({
71
- __type: 'LIST_ITEM' as const,
72
- text: i18n.t(`country.from_code.${countryCode}`),
73
- name: 'address.country',
74
- value: countryCode,
75
- selected: selectedCountryCode === countryCode,
76
- }));
77
- const countryOptions = [
78
- ...priorityCountryOptions,
79
- {
80
- __type: 'BREAK' as const,
81
- },
82
- ...supportedCountryOptions,
83
- ];
84
- const popOverProps = {
85
- zClassName: 'z9',
86
- position: 'bottom_left' as const,
87
- offsetX: 0,
88
- offsetY: 7,
40
+ /* Map stored, nullable address data to the strings used by editable controls. */
41
+ export function addressFormValues(address?: Partial<AddressObj> | null) {
42
+ return {
43
+ line1: address?.line1 || '',
44
+ line2: address?.line2 || '',
45
+ postalCode: address?.postalCode || '',
46
+ city: address?.city || '',
47
+ state: address?.state || '',
48
+ country: address?.country || '',
89
49
  };
50
+ }
90
51
 
91
- return {
92
- listData: [
93
- {
94
- __type: 'input' as const,
95
- item: {
96
- name: 'address.line1',
97
- label: i18n.t('form.line1'),
98
- placeholder: i18n.t('form.line1_ph'),
99
- },
52
+ /* Define the reusable address fields with the existing grouped country popover. */
53
+ export function makeAddressSchema(address?: Partial<AddressObj> | null, popOverDesignClassName = 'w_400') {
54
+ return defineForm({
55
+ schema: z.object({ address: makeAddressValueSchema() }),
56
+ defaults: { address: addressFormValues(address) },
57
+ fields: {
58
+ 'address.line1': {
59
+ kind: 'input',
60
+ label: i18n.t('c.line1'),
61
+ placeholder: i18n.t('c.line1_ph'),
100
62
  },
101
- {
102
- __type: 'input' as const,
103
- item: {
104
- name: 'address.line2',
105
- label: i18n.t('form.line2_optional'),
106
- placeholder: i18n.t('form.line2_ph'),
107
- },
63
+ 'address.line2': {
64
+ kind: 'input',
65
+ label: i18n.t('c.line2_optional'),
66
+ placeholder: i18n.t('c.line2_ph'),
108
67
  },
109
- {
110
- __type: 'input' as const,
111
- item: {
112
- name: 'address.postalCode',
113
- label: i18n.t('form.postalCode'),
114
- placeholder:
115
- currentAddress?.postalCode || i18n.t('form.postalCode_ph'),
116
- },
68
+ 'address.postalCode': {
69
+ kind: 'input',
70
+ label: i18n.t('c.postalCode'),
71
+ placeholder: address?.postalCode || i18n.t('c.postalCode_ph'),
117
72
  },
118
- {
119
- __type: 'group' as const,
120
- item: {
121
- items: [
122
- {
123
- __type: 'input' as const,
124
- item: {
125
- name: 'address.city',
126
- maxLength: MIN_LEN_VALUES.city,
127
- label: i18n.t('form.city'),
128
- placeholder: currentAddress?.city || i18n.t('form.city_ph'),
129
- },
130
- },
131
- {
132
- __type: 'input' as const,
133
- item: {
134
- name: 'address.state',
135
- maxLength: MIN_LEN_VALUES.state,
136
- label: i18n.t('form.state'),
137
- placeholder: currentAddress?.state || '',
138
- },
139
- },
140
- ],
141
- },
73
+ 'address.city': {
74
+ kind: 'input',
75
+ maxLength: 100,
76
+ label: i18n.t('c.city'),
77
+ placeholder: address?.city || i18n.t('c.city_ph'),
78
+ },
79
+ 'address.state': {
80
+ kind: 'input',
81
+ maxLength: 32,
82
+ label: i18n.t('c.state'),
83
+ placeholder: address?.state || '',
142
84
  },
143
- {
144
- __type: 'input_click' as const,
145
- forceClickId: 'input_click_address.country',
146
- label: i18n.t('form.country'),
147
- item: {
148
- label: i18n.t('form.country'),
149
- locked: true,
150
- focused: focusedName === formId + '_address.country',
151
- name: 'address.country',
152
- getter: (value: string) =>
153
- value ? i18n.t(`country.from_code.${value}`) : '',
154
- placeholder: selectedCountryCode || i18n.t('form.country_ph'),
85
+ 'address.country': {
86
+ kind: 'click',
87
+ label: i18n.t('c.country'),
88
+ getter: (value: string) => (SUPPORTED_COUNTRIES.includes(value) ? i18n.t(`country.from_code.${value}`) : value),
89
+ resolve: ({ values }) => ({
90
+ rightText:
91
+ values.address.country && !SUPPORTED_COUNTRIES.includes(values.address.country.trim()) ? i18n.t('error.20099') : undefined,
92
+ placeholder: values.address.country || i18n.t('c.country_ph'),
155
93
  popOverProps: {
156
- ...popOverProps,
157
- id: formId + '_address.country',
94
+ id: 'address.country',
95
+ zClassName: 'z9',
96
+ position: 'bottom_left',
97
+ offsetX: 0,
98
+ offsetY: 7,
158
99
  doNotFixToBottom: true,
159
100
  iface: {
160
101
  name: 'PO_LIST',
161
102
  variables: {
162
- designClassName: popOverDesignClassName ?? 'w_400',
103
+ designClassName: popOverDesignClassName,
163
104
  className: 'max_h_50vh',
164
- initialState: {
165
- 'address.country': selectedCountryCode,
166
- },
167
- options: countryOptions,
105
+ initialState: { 'address.country': values.address.country },
106
+ options: [
107
+ ...['US', 'MX', 'CA'].map((country) => ({
108
+ id: `priority_country_${country}`,
109
+ __type: 'LIST_ITEM' as const,
110
+ text: i18n.t(`country.from_code.${country}`),
111
+ name: 'address.country',
112
+ value: country,
113
+ selected: values.address.country === country,
114
+ })),
115
+ { __type: 'BREAK' as const },
116
+ ...SUPPORTED_COUNTRIES.map((country) => ({
117
+ id: `country_${country}`,
118
+ __type: 'LIST_ITEM' as const,
119
+ text: i18n.t(`country.from_code.${country}`),
120
+ name: 'address.country',
121
+ value: country,
122
+ selected: values.address.country === country,
123
+ })),
124
+ ],
168
125
  },
169
126
  },
170
127
  },
171
- },
128
+ }),
172
129
  },
130
+ },
131
+ layout: [
132
+ 'address.line1',
133
+ 'address.line2',
134
+ 'address.postalCode',
135
+ { kind: 'row', fields: ['address.city', 'address.state'] },
136
+ 'address.country',
173
137
  ],
174
- rules: [
175
- makeAddressRequiredRule('line1', 'form.line1'),
176
- makeAddressRequiredRule('postalCode', 'form.postalCode'),
177
- makeAddressRequiredRule('city', 'form.city'),
178
- makeAddressRequiredRule('state', 'form.state'),
179
- makeAddressRequiredRule('country', 'form.country'),
180
- ],
181
- };
138
+ });
182
139
  }
@@ -0,0 +1,35 @@
1
+ export const AUTH_POPUP_MESSAGE_TYPE = 'marketday-auth-complete';
2
+
3
+ export interface AuthPopupCompletionMessage {
4
+ popupId: string;
5
+ primaryOrganizationId?: string;
6
+ type: typeof AUTH_POPUP_MESSAGE_TYPE;
7
+ }
8
+
9
+ /*
10
+ * Return the completion route for one valid UUID-bound authentication popup.
11
+ */
12
+ export function getAuthPopupCompletionPath(popupId?: string | null): string | null {
13
+ if (!popupId || !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(popupId)) {
14
+ return null;
15
+ }
16
+
17
+ return `/auth/popup-complete?${new URLSearchParams({ popupId })}`;
18
+ }
19
+
20
+ /*
21
+ * Return whether a cross-window message completes the currently open
22
+ * authentication popup.
23
+ */
24
+ export function isAuthPopupCompletionMessage(value: unknown, popupId: string): value is AuthPopupCompletionMessage {
25
+ if (!value || typeof value !== 'object') {
26
+ return false;
27
+ }
28
+
29
+ const message = value as Partial<AuthPopupCompletionMessage>;
30
+ return (
31
+ message.type === AUTH_POPUP_MESSAGE_TYPE &&
32
+ message.popupId === popupId &&
33
+ (message.primaryOrganizationId === undefined || typeof message.primaryOrganizationId === 'string')
34
+ );
35
+ }
@@ -0,0 +1,45 @@
1
+ const FILE_CONSTANTS = {
2
+ scriptUrl: 'https://accounts.google.com/gsi/client',
3
+ scriptTimeoutMs: 10000,
4
+ };
5
+
6
+ let googleScriptPromise: Promise<void> | null = null;
7
+
8
+ /*
9
+ * Return the manual OAuth client after the shared Google SDK loads.
10
+ */
11
+ function getGoogleOAuthClient(): unknown {
12
+ return (globalThis as typeof globalThis & { google?: { accounts?: { oauth2?: unknown } } }).google?.accounts?.oauth2;
13
+ }
14
+
15
+ /*
16
+ * Load one shared Google SDK for manual OAuth, with a bounded failure path.
17
+ */
18
+ export function loadGoogleIdentityServices(): Promise<void> {
19
+ if (getGoogleOAuthClient()) return Promise.resolve();
20
+ if (googleScriptPromise) return googleScriptPromise;
21
+ googleScriptPromise = new Promise<void>((resolve, reject) => {
22
+ const script = document.createElement('script');
23
+ const timer = globalThis.setTimeout(() => finish(false), FILE_CONSTANTS.scriptTimeoutMs);
24
+ /* Clear script listeners and allow a later attempt after a failed SDK load. */
25
+ function finish(success: boolean) {
26
+ globalThis.clearTimeout(timer);
27
+ script.onload = null;
28
+ script.onerror = null;
29
+ if (success) {
30
+ resolve();
31
+ } else {
32
+ script.remove();
33
+ googleScriptPromise = null;
34
+ reject(new Error('Google Identity Services unavailable'));
35
+ }
36
+ }
37
+ script.src = FILE_CONSTANTS.scriptUrl;
38
+ script.async = true;
39
+ script.defer = true;
40
+ script.onload = () => finish(!!getGoogleOAuthClient());
41
+ script.onerror = () => finish(false);
42
+ document.body.appendChild(script);
43
+ });
44
+ return googleScriptPromise;
45
+ }