@jsb188/auth-web 1.1.2
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.
- package/package.json +29 -0
- package/src/hooks/use-auth-popup.ts +167 -0
- package/src/hooks/use-google-one-tap.ts +138 -0
- package/src/hooks/use-oauth.tsx +381 -0
- package/src/main.ts +21 -0
- package/src/modules/AssignRequiredAuth.tsx +274 -0
- package/src/modules/AuthFormUI.tsx +45 -0
- package/src/modules/AuthForms-SSO.tsx +209 -0
- package/src/modules/AuthForms.tsx +914 -0
- package/src/modules/AuthGate.tsx +123 -0
- package/src/modules/ConfirmPublicFormEmailCode.tsx +144 -0
- package/src/modules/InlineAuthForm.tsx +459 -0
- package/src/modules/ManagedSessions.tsx +115 -0
- package/src/modules/OtherAuthButtons.tsx +118 -0
- package/src/modules/_helpers.ts +200 -0
- package/src/modules/schemas.ts +177 -0
- package/src/schemas/account.ts +114 -0
- package/src/schemas/address.ts +133 -0
- package/src/schemas/index.ts +2 -0
- package/src/ui/ManagedSessionUI.tsx +95 -0
- package/src/utils/authPopup.ts +95 -0
- package/src/utils/googleIdentity.ts +100 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { runWithAuthSessionBrowserLock } from '@jsb188/app/utils/authSession.ts';
|
|
2
|
+
import { clearGoogleAutoSignInSuppression } from '../utils/googleIdentity';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Types
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type AuthPageName = 'signin' | 'signin_success' | 'signup' | 'signup_success' | 'reset_password' | 'welcome';
|
|
9
|
+
|
|
10
|
+
export type OAuthSessionAuthenticationInput = {
|
|
11
|
+
provider: 'GOOGLE' | 'MICROSOFT';
|
|
12
|
+
oauthToken?: string | null;
|
|
13
|
+
idToken?: string | null;
|
|
14
|
+
ssoConnectionId?: string | null;
|
|
15
|
+
formPublicId?: string | null;
|
|
16
|
+
authenticateOnly?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type OAuthSessionAuthenticationResult = {
|
|
20
|
+
auth: any | null;
|
|
21
|
+
error: any | null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type AuthenticationSessionHydrationResult = {
|
|
25
|
+
auth: any | null;
|
|
26
|
+
error: any | null;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const DEFAULT_AUTH_ROUTES: Record<AuthPageName, string> = {
|
|
30
|
+
signin: '/auth/signin',
|
|
31
|
+
signin_success: '/auth/success',
|
|
32
|
+
signup: '/auth/signup',
|
|
33
|
+
signup_success: '/auth/signup-finished',
|
|
34
|
+
reset_password: '/auth/reset-password',
|
|
35
|
+
welcome: '/auth/welcome',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Helper; get route path
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
export function getAuthRoute(pageName: AuthPageName, authRoutesMap?: Record<AuthPageName, string>, queryStr?: string) {
|
|
43
|
+
const pathname = authRoutesMap?.[pageName] || DEFAULT_AUTH_ROUTES[pageName] || 'auth';
|
|
44
|
+
return queryStr ? `${pathname}?${queryStr}` : pathname;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
* Encode one validated post-authentication destination for auth route links.
|
|
49
|
+
*/
|
|
50
|
+
export function getAuthReturnQuery(returnTo?: string | null) {
|
|
51
|
+
return returnTo ? new URLSearchParams({ returnTo }).toString() : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
* Accept only a same-site public-form destination and extract its campaign id.
|
|
56
|
+
*/
|
|
57
|
+
export function getPublicFormReturnContext(value?: string | null) {
|
|
58
|
+
const match = value?.match(/^\/form\/([A-Za-z0-9]+)$/);
|
|
59
|
+
return {
|
|
60
|
+
formPublicId: match?.[1] || null,
|
|
61
|
+
returnTo: match?.[0] || null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* Preserve only explicitly supported account-auth destinations. MCP consent is
|
|
67
|
+
* account authentication and must never acquire public-form respondent semantics.
|
|
68
|
+
*/
|
|
69
|
+
export function getAuthReturnContext(value?: string | null) {
|
|
70
|
+
const publicForm = getPublicFormReturnContext(value);
|
|
71
|
+
if (publicForm.returnTo) return publicForm;
|
|
72
|
+
const consent = value?.match(
|
|
73
|
+
/^\/auth\/mcp-consent\?request=([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$/,
|
|
74
|
+
);
|
|
75
|
+
return {
|
|
76
|
+
formPublicId: null,
|
|
77
|
+
returnTo: consent ? `/auth/mcp-consent?request=${consent[1]}` : value === '/auth/mcp-connections' ? value : null,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/*
|
|
82
|
+
* Post one login credential to the same-origin HttpOnly-cookie endpoint.
|
|
83
|
+
*/
|
|
84
|
+
async function postSignInSessionCredential(name: 'bootstrapToken' | 'token', value: string): Promise<boolean> {
|
|
85
|
+
try {
|
|
86
|
+
const response = await fetch('/auth/session', {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
credentials: 'same-origin',
|
|
89
|
+
headers: {
|
|
90
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
91
|
+
},
|
|
92
|
+
body: new URLSearchParams({
|
|
93
|
+
[name]: value,
|
|
94
|
+
managedSessionVersion: '1',
|
|
95
|
+
}),
|
|
96
|
+
});
|
|
97
|
+
return response.ok;
|
|
98
|
+
} catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/*
|
|
104
|
+
* Authenticate one OAuth provider credential through the same-origin BFF and
|
|
105
|
+
* atomically persist the resulting HttpOnly managed-session cookie.
|
|
106
|
+
*/
|
|
107
|
+
export async function authenticateOAuthSignInSession(
|
|
108
|
+
input: OAuthSessionAuthenticationInput,
|
|
109
|
+
signal?: AbortSignal,
|
|
110
|
+
): Promise<OAuthSessionAuthenticationResult> {
|
|
111
|
+
if (typeof fetch !== 'function') {
|
|
112
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
117
|
+
signal?.throwIfAborted();
|
|
118
|
+
const body = new URLSearchParams({
|
|
119
|
+
provider: input.provider,
|
|
120
|
+
managedSessionVersion: '1',
|
|
121
|
+
...(input.oauthToken ? { oauthToken: input.oauthToken } : {}),
|
|
122
|
+
...(input.idToken ? { idToken: input.idToken } : {}),
|
|
123
|
+
...(input.ssoConnectionId ? { ssoConnectionId: input.ssoConnectionId } : {}),
|
|
124
|
+
...(input.formPublicId ? { formPublicId: input.formPublicId } : {}),
|
|
125
|
+
...(input.authenticateOnly ? { authenticateOnly: '1' } : {}),
|
|
126
|
+
});
|
|
127
|
+
const response = await fetch('/auth/session', {
|
|
128
|
+
signal,
|
|
129
|
+
method: 'POST',
|
|
130
|
+
credentials: 'same-origin',
|
|
131
|
+
headers: {
|
|
132
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
133
|
+
},
|
|
134
|
+
body,
|
|
135
|
+
});
|
|
136
|
+
const result = await response.json().catch(() => null);
|
|
137
|
+
if (!response.ok || !result?.ok) {
|
|
138
|
+
return {
|
|
139
|
+
auth: null,
|
|
140
|
+
error: result?.error || { errorCode: 'network_error' },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (input.provider === 'GOOGLE' && (result.auth?.account?.id || result.auth?.respondentToken)) {
|
|
144
|
+
clearGoogleAutoSignInSuppression();
|
|
145
|
+
}
|
|
146
|
+
return { auth: result.auth || null, error: null };
|
|
147
|
+
});
|
|
148
|
+
} catch {
|
|
149
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* Rehydrate browser authentication from the shared HttpOnly managed-session
|
|
155
|
+
* cookie after a same-origin popup establishes it.
|
|
156
|
+
*/
|
|
157
|
+
export async function hydrateAuthenticationSession(): Promise<AuthenticationSessionHydrationResult> {
|
|
158
|
+
if (typeof fetch !== 'function') {
|
|
159
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
164
|
+
const response = await fetch('/auth/session', {
|
|
165
|
+
method: 'POST',
|
|
166
|
+
credentials: 'same-origin',
|
|
167
|
+
headers: {
|
|
168
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
169
|
+
},
|
|
170
|
+
body: new URLSearchParams({ hydrate: '1' }),
|
|
171
|
+
});
|
|
172
|
+
const result = await response.json().catch(() => null);
|
|
173
|
+
if (!response.ok || !result?.ok || !result.auth?.token) {
|
|
174
|
+
return {
|
|
175
|
+
auth: null,
|
|
176
|
+
error: result?.error || { errorCode: 'network_error' },
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return { auth: result.auth, error: null };
|
|
180
|
+
});
|
|
181
|
+
} catch {
|
|
182
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
* Persist an OAuth or phone login before navigating without a bearer-token query.
|
|
188
|
+
*/
|
|
189
|
+
export async function persistSignInSession(token: string, bootstrapToken?: string | null): Promise<boolean> {
|
|
190
|
+
if (typeof fetch !== 'function' || (!token && !bootstrapToken)) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
195
|
+
if (bootstrapToken && (await postSignInSessionCredential('bootstrapToken', bootstrapToken))) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
return token ? await postSignInSessionCredential('token', token) : false;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { defineForm } from '@jsb188/form/libs/defineForm';
|
|
3
|
+
import { rules } from '@jsb188/form/libs/rules';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
export type SignInFormValues = z.input<ReturnType<typeof makeSignInSchema>['schema']>;
|
|
7
|
+
|
|
8
|
+
export interface SignInFormContext {
|
|
9
|
+
hidePassword: boolean;
|
|
10
|
+
lastIdentifier?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* Define the current password and matching new password fields for account changes and token resets.
|
|
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
|
+
}
|
|
48
|
+
|
|
49
|
+
/*
|
|
50
|
+
* Define a matching password pair when an account does not yet have a password.
|
|
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
|
+
}
|
|
76
|
+
|
|
77
|
+
/*
|
|
78
|
+
* Define identifier discovery and the password step without applying new-password rules to existing passwords.
|
|
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
|
+
}
|
|
106
|
+
|
|
107
|
+
/*
|
|
108
|
+
* Define the email sign-up fields and the shared password strength and confirmation rules.
|
|
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
|
+
}
|
|
142
|
+
|
|
143
|
+
/*
|
|
144
|
+
* Define the email field shared by verification, recovery, and organization sign-in discovery.
|
|
145
|
+
*/
|
|
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
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/*
|
|
162
|
+
* Define the phone field used to request a sign-in verification code.
|
|
163
|
+
*/
|
|
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
|
+
});
|
|
177
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { SUPPORTED_LANGUAGES } from '@jsb188/app/constants/app.ts';
|
|
2
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
3
|
+
import type { AccountData, AccountSettings } from '@jsb188/app/types/auth.d.ts';
|
|
4
|
+
import { formatPhoneNumber } from '@jsb188/app/utils/string.ts';
|
|
5
|
+
import { defineForm } from '@jsb188/form/libs/defineForm';
|
|
6
|
+
import type { OpenModalPopUpFn } from '@jsb188/react/states';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
|
|
9
|
+
/* Define editable account values and the existing verification action controls. */
|
|
10
|
+
export function makeEditAccountSchema(
|
|
11
|
+
account: AccountData | null,
|
|
12
|
+
settings: AccountSettings | null,
|
|
13
|
+
openModalPopUp: OpenModalPopUpFn,
|
|
14
|
+
) {
|
|
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',
|
|
37
|
+
},
|
|
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 }) => ({
|
|
51
|
+
popOverProps: {
|
|
52
|
+
id: 'edit_account_settings.language',
|
|
53
|
+
zClassName: 'z9',
|
|
54
|
+
position: 'bottom_left',
|
|
55
|
+
offsetX: 0,
|
|
56
|
+
offsetY: 7,
|
|
57
|
+
iface: {
|
|
58
|
+
name: 'PO_LIST',
|
|
59
|
+
variables: {
|
|
60
|
+
designClassName: 'w_400',
|
|
61
|
+
className: 'max_h_40vh',
|
|
62
|
+
options: SUPPORTED_LANGUAGES.map((language) => ({
|
|
63
|
+
__type: 'LIST_ITEM' as const,
|
|
64
|
+
text: i18n.t(`c.lang.${language}`),
|
|
65
|
+
name: 'settings.language',
|
|
66
|
+
value: language,
|
|
67
|
+
selected: (values.settings.language || 'en') === language,
|
|
68
|
+
})),
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
},
|
|
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' }),
|
|
83
|
+
},
|
|
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' }),
|
|
93
|
+
},
|
|
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' }),
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
layout: [
|
|
107
|
+
{ kind: 'row', fields: ['profile.firstName', 'profile.lastName'] },
|
|
108
|
+
'settings.language',
|
|
109
|
+
'change_phoneNumber',
|
|
110
|
+
'change_emailAddress',
|
|
111
|
+
'change_password',
|
|
112
|
+
],
|
|
113
|
+
});
|
|
114
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { SUPPORTED_COUNTRIES } from '@jsb188/app/constants/app.ts';
|
|
2
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
3
|
+
import type { AddressObj } from '@jsb188/app/types/other.d.ts';
|
|
4
|
+
import { defineForm } from '@jsb188/form/libs/defineForm';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
/* Validate an optional address, requiring its core fields once an address is entered. */
|
|
8
|
+
export function makeAddressValueSchema() {
|
|
9
|
+
return z
|
|
10
|
+
.object({
|
|
11
|
+
line1: z.string(),
|
|
12
|
+
line2: z.string(),
|
|
13
|
+
city: z.string(),
|
|
14
|
+
state: z.string(),
|
|
15
|
+
postalCode: z.string(),
|
|
16
|
+
country: z.string(),
|
|
17
|
+
})
|
|
18
|
+
.superRefine((address, context) => {
|
|
19
|
+
const hasAddress = [address.line1, address.line2, address.city, address.state, address.postalCode].some((value) => value.trim());
|
|
20
|
+
if (!hasAddress) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for (const field of ['line1', 'postalCode', 'city', 'state', 'country'] as const) {
|
|
25
|
+
if (!address[field].trim()) {
|
|
26
|
+
context.addIssue({
|
|
27
|
+
code: z.ZodIssueCode.custom,
|
|
28
|
+
path: [field],
|
|
29
|
+
message: i18n.t('error.msg_value_required', { value: i18n.t(`c.${field}`) }),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* Map stored, nullable address data to the strings used by editable controls. */
|
|
37
|
+
export function addressFormValues(address?: Partial<AddressObj> | null) {
|
|
38
|
+
return {
|
|
39
|
+
line1: address?.line1 || '',
|
|
40
|
+
line2: address?.line2 || '',
|
|
41
|
+
postalCode: address?.postalCode || '',
|
|
42
|
+
city: address?.city || '',
|
|
43
|
+
state: address?.state || '',
|
|
44
|
+
country: address?.country || '',
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* Define the reusable address fields with the existing grouped country popover. */
|
|
49
|
+
export function makeAddressSchema(address?: Partial<AddressObj> | null, popOverDesignClassName = 'w_400') {
|
|
50
|
+
return defineForm({
|
|
51
|
+
schema: z.object({ address: makeAddressValueSchema() }),
|
|
52
|
+
defaults: { address: addressFormValues(address) },
|
|
53
|
+
fields: {
|
|
54
|
+
'address.line1': {
|
|
55
|
+
kind: 'input',
|
|
56
|
+
label: i18n.t('c.line1'),
|
|
57
|
+
placeholder: i18n.t('c.line1_ph'),
|
|
58
|
+
},
|
|
59
|
+
'address.line2': {
|
|
60
|
+
kind: 'input',
|
|
61
|
+
label: i18n.t('c.line2_optional'),
|
|
62
|
+
placeholder: i18n.t('c.line2_ph'),
|
|
63
|
+
},
|
|
64
|
+
'address.postalCode': {
|
|
65
|
+
kind: 'input',
|
|
66
|
+
label: i18n.t('c.postalCode'),
|
|
67
|
+
placeholder: address?.postalCode || i18n.t('c.postalCode_ph'),
|
|
68
|
+
},
|
|
69
|
+
'address.city': {
|
|
70
|
+
kind: 'input',
|
|
71
|
+
maxLength: 100,
|
|
72
|
+
label: i18n.t('c.city'),
|
|
73
|
+
placeholder: address?.city || i18n.t('c.city_ph'),
|
|
74
|
+
},
|
|
75
|
+
'address.state': {
|
|
76
|
+
kind: 'input',
|
|
77
|
+
maxLength: 32,
|
|
78
|
+
label: i18n.t('c.state'),
|
|
79
|
+
placeholder: address?.state || '',
|
|
80
|
+
},
|
|
81
|
+
'address.country': {
|
|
82
|
+
kind: 'click',
|
|
83
|
+
label: i18n.t('c.country'),
|
|
84
|
+
getter: (value: string) => (value ? i18n.t(`country.from_code.${value}`) : ''),
|
|
85
|
+
resolve: ({ values }) => ({
|
|
86
|
+
placeholder: values.address.country || i18n.t('c.country_ph'),
|
|
87
|
+
popOverProps: {
|
|
88
|
+
id: 'address.country',
|
|
89
|
+
zClassName: 'z9',
|
|
90
|
+
position: 'bottom_left',
|
|
91
|
+
offsetX: 0,
|
|
92
|
+
offsetY: 7,
|
|
93
|
+
doNotFixToBottom: true,
|
|
94
|
+
iface: {
|
|
95
|
+
name: 'PO_LIST',
|
|
96
|
+
variables: {
|
|
97
|
+
designClassName: popOverDesignClassName,
|
|
98
|
+
className: 'max_h_50vh',
|
|
99
|
+
initialState: { 'address.country': values.address.country },
|
|
100
|
+
options: [
|
|
101
|
+
...['US', 'MX', 'CA'].map((country) => ({
|
|
102
|
+
id: `priority_country_${country}`,
|
|
103
|
+
__type: 'LIST_ITEM' as const,
|
|
104
|
+
text: i18n.t(`country.from_code.${country}`),
|
|
105
|
+
name: 'address.country',
|
|
106
|
+
value: country,
|
|
107
|
+
selected: values.address.country === country,
|
|
108
|
+
})),
|
|
109
|
+
{ __type: 'BREAK' as const },
|
|
110
|
+
...SUPPORTED_COUNTRIES.map((country) => ({
|
|
111
|
+
id: `country_${country}`,
|
|
112
|
+
__type: 'LIST_ITEM' as const,
|
|
113
|
+
text: i18n.t(`country.from_code.${country}`),
|
|
114
|
+
name: 'address.country',
|
|
115
|
+
value: country,
|
|
116
|
+
selected: values.address.country === country,
|
|
117
|
+
})),
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
}),
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
layout: [
|
|
126
|
+
'address.line1',
|
|
127
|
+
'address.line2',
|
|
128
|
+
'address.postalCode',
|
|
129
|
+
{ kind: 'row', fields: ['address.city', 'address.state'] },
|
|
130
|
+
'address.country',
|
|
131
|
+
],
|
|
132
|
+
});
|
|
133
|
+
}
|