@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,33 +1,30 @@
1
1
  import { APP_EMAILS } from '@jsb188/app/constants/app.ts';
2
2
  import i18n from '@jsb188/app/i18n/index.ts';
3
3
  import {
4
+ useAuthenticateAsAgent,
4
5
  useCheckUsernameOrEmail,
5
6
  useConfirmPhoneVerificationCode,
6
7
  useRequestTokenizedEmail,
7
8
  useSendPhoneVerificationCode,
8
9
  useSignUpWithEmail,
9
10
  } from '@jsb188/graphql/hooks/use-auth-mtn';
10
- import { SMSCodeInput } from '@jsb188/react-web/modules/Form';
11
- import SchemaForm from '@jsb188/react-web/modules/SchemaForm';
11
+ import { SMSCodeInput } from '@jsb188/form/modules/Form';
12
+ import SchemaForm from '@jsb188/form/libs/SchemaForm';
13
+ import { submissionResult } from '@jsb188/form/libs/validation';
14
+ import type { FormActions, SubmitResult } from '@jsb188/form/libs/SchemaForm.types';
12
15
  import { FullWidthButton } from '@jsb188/react-web/ui/Button';
13
- import { FormBreak } from '@jsb188/react-web/ui/Form';
16
+ import { FormBreak } from '@jsb188/form/ui/Form';
14
17
  import Markdown, { TextWithLinks } from '@jsb188/react-web/ui/Markdown';
15
- import {
16
- ModalErrorMessage,
17
- ModalSimpleContent,
18
- } from '@jsb188/react-web/ui/ModalUI';
19
- import { useMemo, useState } from 'react';
18
+ import { ModalErrorMessage, ModalSimpleContent } from '@jsb188/react-web/ui/ModalUI';
19
+ import { useEffect, useMemo, useRef, useState } from 'react';
20
20
  import { Link, useNavigate } from 'react-router';
21
+ import { getAuthPopupCompletionPath } from '../utils/authPopup.ts';
21
22
  import type { AuthPageName } from './_helpers';
22
- import { getAuthRoute, persistSignInSession } from './_helpers';
23
+ import { getAuthReturnQuery, getAuthRoute, persistSignInSession } from './_helpers';
23
24
  import { SsoSignIn } from './AuthForms-SSO';
25
+ import { ContinueWithGoogle, ContinueWithMicrosoft, ContinueWithPhone, ContinueWithSso } from './OtherAuthButtons';
24
26
  import {
25
- ContinueWithGoogle,
26
- ContinueWithMicrosoft,
27
- ContinueWithPhone,
28
- ContinueWithSso,
29
- } from './OtherAuthButtons';
30
- import {
27
+ type SignInFormValues,
31
28
  makeRequestTokenizedEmailSchema,
32
29
  makeSendPhoneVerificationSchema,
33
30
  makeSignInSchema,
@@ -50,44 +47,78 @@ export const meta = [
50
47
 
51
48
  interface AuthFormProps {
52
49
  appNamespace: string;
50
+ initialMethod?: 'PHONE';
53
51
  initialError?: any;
54
52
  initialFormData?: any;
55
53
  authRoutesMap?: Record<AuthPageName, string>;
54
+ popupId?: string | null;
55
+ popupMode?: boolean;
56
+ returnTo?: string | null;
57
+ // Development-only: when true, show the password-less "AI agents Enter"
58
+ // button. Provided by the host app's sign-in loader; the server reports it
59
+ // as true only on a local development database.
60
+ agentLoginAvailable?: boolean;
56
61
  setAuthIface: (iface: SignInIface | null) => void;
57
62
 
58
63
  // Check if needed still
59
64
  onError?: (error: any) => void;
60
65
  }
61
66
 
62
- function SignInMain(p: AuthFormProps) {
63
- const { authRoutesMap, initialError, initialFormData, setAuthIface } = p;
67
+ /*
68
+ * Preserve return and popup context across one authentication navigation.
69
+ */
70
+ function getAuthNavigationQuery(returnTo?: string | null, popupMode?: boolean, popupId?: string | null) {
71
+ const searchParams = new URLSearchParams(getAuthReturnQuery(returnTo));
72
+ if (popupMode && popupId) {
73
+ searchParams.set('popup', '1');
74
+ searchParams.set('popupId', popupId);
75
+ }
76
+
77
+ return searchParams.toString() || undefined;
78
+ }
79
+
80
+ /*
81
+ * Return the popup handoff route or the standard post-authentication route.
82
+ */
83
+ function getSignInSuccessRoute(p: Pick<AuthFormProps, 'authRoutesMap' | 'popupId' | 'popupMode' | 'returnTo'>) {
84
+ const popupCompletionPath = p.popupMode ? getAuthPopupCompletionPath(p.popupId) : null;
85
+ if (popupCompletionPath) {
86
+ return popupCompletionPath;
87
+ }
88
+
89
+ return getAuthRoute('signin_success', p.authRoutesMap, getAuthReturnQuery(p.returnTo));
90
+ }
91
+
92
+ /*
93
+ * Discover existing accounts before choosing the verification screen.
94
+ */
95
+ const SignInMain = (p: AuthFormProps) => {
96
+ const { authRoutesMap, initialError, initialFormData, popupId, popupMode, returnTo, setAuthIface } = p;
64
97
  const navigate = useNavigate();
65
98
  const [waitingForPostRequest, setWaitingForPostRequest] = useState(false);
99
+ const mounted = useRef(true);
66
100
  const signInSchema = useMemo(makeSignInSchema, []);
67
- const {
68
- saving,
69
- checkUsernameOrEmail,
70
- dataForSchema,
71
- setDataForSchema,
72
- error,
73
- setError,
74
- resetErrors,
75
- } = useCheckUsernameOrEmail({
101
+ const { saving, checkUsernameOrEmail, dataForSchema, setDataForSchema, error, setError, resetErrors } = useCheckUsernameOrEmail({
76
102
  initialError,
77
- initialFormData,
103
+ initialFormData: {
104
+ ...initialFormData,
105
+ usernameOrEmail: initialFormData?.identifier || initialFormData?.usernameOrEmail || '',
106
+ },
78
107
  });
79
108
 
80
109
  const hidePassword = dataForSchema.hidePassword;
81
- const onFormValuesChange = (
82
- formValues: any,
83
- _currentValues: any,
84
- setFormValues: (val: any) => void,
85
- ) => {
110
+
111
+ useEffect(() => {
112
+ mounted.current = true;
113
+ return () => {
114
+ mounted.current = false;
115
+ };
116
+ }, []);
117
+
118
+ /* Return to identifier discovery when the previously resolved identifier changes. */
119
+ const onFormValuesChange = (formValues: SignInFormValues, { setValues }: FormActions<SignInFormValues>) => {
86
120
  const hasInput = !!formValues.identifier?.trim();
87
- if (
88
- !hidePassword &&
89
- formValues.identifier !== dataForSchema.lastIdentifier
90
- ) {
121
+ if (!hidePassword && formValues.identifier !== dataForSchema.lastIdentifier) {
91
122
  setDataForSchema({
92
123
  ...dataForSchema,
93
124
  hasInput,
@@ -95,7 +126,7 @@ function SignInMain(p: AuthFormProps) {
95
126
  hidePassword: true,
96
127
  });
97
128
 
98
- setFormValues({
129
+ setValues({
99
130
  ...formValues,
100
131
  password: '',
101
132
  });
@@ -107,129 +138,156 @@ function SignInMain(p: AuthFormProps) {
107
138
  }
108
139
  };
109
140
 
110
- async function onSubmit(
111
- formValues: any,
112
- _currentValues: any,
113
- setFormValues: any,
114
- ) {
115
- const identifier = formValues.identifier?.trim();
141
+ /* Resolve the account before authorizing the native password-manager-compatible form POST. */
142
+ const onSubmit = async (
143
+ formValues: SignInFormValues,
144
+ { getValues, setValues }: FormActions<SignInFormValues>,
145
+ ): Promise<SubmitResult<SignInFormValues>> => {
146
+ const identifier = formValues.identifier.trim();
116
147
  if (hidePassword) {
117
- const { checkUsernameOrEmail: result } = await checkUsernameOrEmail({
148
+ const response = await checkUsernameOrEmail({
118
149
  variables: {
119
150
  usernameOrEmail: identifier,
120
151
  authCheck: true,
121
152
  },
122
153
  });
154
+ if (!mounted.current) {
155
+ return { status: 'continue' };
156
+ }
157
+ if (getValues().identifier.trim() !== identifier) {
158
+ resetErrors();
159
+ return { status: 'continue' };
160
+ }
161
+ const submission = submissionResult(response);
162
+ if (submission.status !== 'success') {
163
+ return submission;
164
+ }
165
+ const result = response?.checkUsernameOrEmail;
123
166
 
124
167
  switch (result?.type) {
125
168
  case 'PHONE':
126
- {
127
- if (!result.taken) {
128
- setError({
129
- message: i18n.t('auth.no_account_with_phone'),
130
- });
131
- return false;
132
- } else if (result?.hasPassword === false) {
133
- setAuthIface('VERIFY_PHONE');
134
- return false;
135
- }
136
- // Do this if you want to redirect to sign up
137
- // setScreen({
138
- // name: 'sign_up',
139
- // email: result.value,
140
- // });
141
- // return false;
169
+ if (!result.taken) {
170
+ return { status: 'error', error: { message: i18n.t('auth.no_account_with_phone') } };
171
+ }
172
+ if (result.hasPassword === false) {
173
+ setAuthIface('VERIFY_PHONE');
174
+ return { status: 'continue' };
142
175
  }
143
176
  break;
144
177
  case 'EMAIL':
145
- {
146
- if (!result.taken) {
147
- setError({
148
- message: i18n.t('auth.no_account_with_email'),
149
- });
150
- // Do this if you want to redirect to sign up
151
- // setScreen({
152
- // name: 'sign_up',
153
- // email: result.value,
154
- // });
155
- return false;
156
- }
178
+ if (!result.taken) {
179
+ return { status: 'error', error: { message: i18n.t('auth.no_account_with_email') } };
157
180
  }
158
181
  break;
159
182
  default:
160
- // This auth type is not supported
161
- console.dev('Unknown auth type:', result);
162
-
163
- if (result?.type) {
164
- setError({
165
- message: i18n.t('auth.not_valid_email_or_phone'),
166
- });
167
- }
168
- return false;
183
+ return { status: 'error', error: { message: i18n.t('auth.not_valid_email_or_phone') } };
169
184
  }
170
185
 
171
186
  setDataForSchema({
172
187
  ...dataForSchema,
173
- lastIdentifier: identifier || '',
174
- hidePassword: !result?.taken,
175
- });
176
-
177
- setFormValues({
178
- ...formValues,
179
- identifier: identifier || '',
188
+ lastIdentifier: identifier,
189
+ hidePassword: false,
180
190
  });
191
+ setValues((currentValues) => ({ ...currentValues, identifier }));
181
192
  } else if (formValues.password && identifier) {
182
193
  setWaitingForPostRequest(true);
183
- return true; // "Do" post form
194
+ return { status: 'native' };
184
195
  }
185
196
 
186
- return false; // Do not post form
187
- }
197
+ return { status: 'continue' };
198
+ };
188
199
 
189
200
  const showSignUpLink = hidePassword && !error;
190
201
 
191
- const onFinishSignIn = async (
192
- token: string,
193
- sessionBootstrapToken?: string | null,
194
- sessionPersisted?: boolean,
195
- ) => {
196
- const persisted =
197
- sessionPersisted ||
198
- (await persistSignInSession(token, sessionBootstrapToken));
202
+ const onFinishSignIn = async (token: string, sessionBootstrapToken?: string | null, sessionPersisted?: boolean) => {
203
+ const persisted = sessionPersisted || (await persistSignInSession(token, sessionBootstrapToken));
199
204
  if (!persisted) {
200
205
  setError({
201
206
  message: i18n.t('error.network_error'),
202
207
  });
203
208
  return false;
204
209
  }
205
- navigate(getAuthRoute('signin_success', authRoutesMap));
210
+ navigate(getSignInSuccessRoute({ authRoutesMap, popupId, popupMode, returnTo }));
206
211
  return true;
207
212
  };
208
213
 
214
+ // Development-only password-less agent login. agentLoginAvailable is
215
+ // resolved server-side (true only on a local development database), so the
216
+ // button never shows in production.
217
+ const agentLoginAvailable = p.agentLoginAvailable === true;
218
+ const { authenticateAsAgent, saving: agentLoginSaving } = useAuthenticateAsAgent({
219
+ onCompleted: async (data: any) => {
220
+ const auth = data?.authenticateAsAgent;
221
+ if (!auth?.token && !auth?.sessionBootstrapToken) {
222
+ setError({ message: i18n.t('error.network_error') });
223
+ return;
224
+ }
225
+ await onFinishSignIn(auth.token, auth.sessionBootstrapToken);
226
+ },
227
+ /* Forward GraphQL failures only when the callback carries an error. */
228
+ onError: (error) => {
229
+ if (error) setError(error);
230
+ },
231
+ });
232
+
233
+ const onClickAgentLogin = (e: { preventDefault: () => void }) => {
234
+ e.preventDefault();
235
+ const identifier = dataForSchema.lastIdentifier?.trim();
236
+ if (identifier) {
237
+ authenticateAsAgent({ variables: { identifier } });
238
+ }
239
+ };
240
+
241
+ // Show the agent button above the password submit button, once the account
242
+ // has been resolved and the password field is visible
243
+ const agentLoginFooter =
244
+ agentLoginAvailable && !hidePassword ? (
245
+ <FullWidthButton
246
+ className="mt_sm"
247
+ preset="bg_secondary"
248
+ loading={agentLoginSaving}
249
+ onClick={onClickAgentLogin}
250
+ >
251
+ {i18n.t('auth.agent_login')}
252
+ </FullWidthButton>
253
+ ) : null;
254
+
209
255
  return (
210
256
  <>
211
- <ContinueWithGoogle onFinishSignIn={onFinishSignIn} />
212
- <ContinueWithMicrosoft onFinishSignIn={onFinishSignIn} />
213
- <ContinueWithSso onClickButton={() => setAuthIface('SSO')} />
257
+ {popupMode ? null : (
258
+ <>
259
+ <ContinueWithGoogle onFinishSignIn={onFinishSignIn} />
260
+ <ContinueWithMicrosoft onFinishSignIn={onFinishSignIn} />
261
+ <ContinueWithSso onClickButton={() => setAuthIface('SSO')} />
262
+ </>
263
+ )}
214
264
  <ContinueWithPhone onClickButton={() => setAuthIface('VERIFY_PHONE')} />
215
265
 
216
- <FormBreak className="mt_md" colorClassName="strong_bf">
217
- {i18n.t('form.or')}
266
+ <FormBreak
267
+ className="mt_md"
268
+ colorClassName="strong_bf"
269
+ >
270
+ {i18n.t('c.or')}
218
271
  </FormBreak>
219
272
 
220
273
  <SchemaForm
221
- doNotPostForm={(fv) => !fv.identifier || !fv.password}
222
- httpMethod="post"
223
- actionUrl={getAuthRoute('signin', authRoutesMap)}
274
+ method="post"
275
+ actionUrl={getAuthRoute('signin', authRoutesMap, getAuthNavigationQuery(returnTo, popupMode, popupId))}
224
276
  className="py_df"
225
- currentData={{ identifier: initialFormData?.identifier || '' }}
226
- saving={saving || waitingForPostRequest}
227
- schema={signInSchema}
228
- dataForSchema={dataForSchema}
277
+ initialValues={{ identifier: initialFormData?.identifier || '' }}
278
+ busy={saving || waitingForPostRequest}
279
+ definition={signInSchema}
280
+ context={dataForSchema}
229
281
  onSubmit={onSubmit}
230
- onFormValuesChange={onFormValuesChange}
282
+ onError={setError}
283
+ onValuesChange={onFormValuesChange}
284
+ FooterComponent={agentLoginFooter}
231
285
  >
232
- <input type="hidden" name="managedSessionVersion" value="1" />
286
+ <input
287
+ type="hidden"
288
+ name="managedSessionVersion"
289
+ value="1"
290
+ />
233
291
  {error && (
234
292
  <ModalErrorMessage
235
293
  error={error}
@@ -239,27 +297,30 @@ function SignInMain(p: AuthFormProps) {
239
297
  )}
240
298
  </SchemaForm>
241
299
 
242
- <div className="a_c">
243
- {showSignUpLink ? (
244
- <Link
245
- className="text ft_sm cl_md link"
246
- to={getAuthRoute('signup', authRoutesMap)}
247
- replace
248
- >
249
- {i18n.t('auth.signup_email')}
250
- </Link>
251
- ) : (
252
- <button
253
- className="text ft_sm cl_md link"
254
- onClick={() => setAuthIface('REQUEST_PASSWORD_RESET')}
255
- >
256
- {i18n.t('auth.forgot_password_')}
257
- </button>
258
- )}
259
- </div>
300
+ {!showSignUpLink || !popupMode ? (
301
+ <div className="a_c">
302
+ {showSignUpLink ? (
303
+ <Link
304
+ className="text_link ft_sm cl_md link"
305
+ to={getAuthRoute('signup', authRoutesMap, getAuthNavigationQuery(returnTo, popupMode, popupId))}
306
+ replace
307
+ >
308
+ {i18n.t('auth.signup_email')}
309
+ </Link>
310
+ ) : (
311
+ <button
312
+ type="button"
313
+ className="text_link ft_sm cl_md link"
314
+ onClick={() => setAuthIface('REQUEST_PASSWORD_RESET')}
315
+ >
316
+ {i18n.t('auth.forgot_password_')}
317
+ </button>
318
+ )}
319
+ </div>
320
+ ) : null}
260
321
  </>
261
322
  );
262
- }
323
+ };
263
324
 
264
325
  /**
265
326
  * Forgot password form
@@ -283,19 +344,13 @@ function RequestTokenizedEmail(p: RequestTokenizedEmailProps) {
283
344
 
284
345
  return {
285
346
  ...schema,
286
- listData: schema.listData.map((obj) => {
287
- if (obj.item.name === 'email') {
288
- return {
289
- ...obj,
290
- item: {
291
- ...obj.item,
292
- placeholder: initialEmailAddress,
293
- },
294
- };
295
- }
296
-
297
- return obj;
298
- }),
347
+ fields: {
348
+ ...schema.fields,
349
+ email: {
350
+ ...schema.fields.email!,
351
+ placeholder: initialEmailAddress,
352
+ },
353
+ },
299
354
  };
300
355
  }, []);
301
356
 
@@ -313,16 +368,18 @@ function RequestTokenizedEmail(p: RequestTokenizedEmailProps) {
313
368
  },
314
369
  });
315
370
 
316
- const onSubmit = (fv: any) => {
317
- requestTokenizedEmail({
371
+ /* Await the authentication request so submission state includes failures and completion. */
372
+ const onSubmit = async (fv: { email: string }) => {
373
+ const result = await requestTokenizedEmail({
318
374
  variables: {
319
375
  ...fv,
320
376
  requestType,
321
377
  },
322
378
  });
379
+ return submissionResult(result);
323
380
  };
324
381
 
325
- let instructionText;
382
+ let instructionText: string | undefined;
326
383
  switch (requestType) {
327
384
  case 'PASSWORD_RESET':
328
385
  instructionText = i18n.t('auth.request_reset_password_instr');
@@ -335,18 +392,24 @@ function RequestTokenizedEmail(p: RequestTokenizedEmailProps) {
335
392
 
336
393
  return (
337
394
  <SchemaForm
338
- saving={saving}
339
- schema={tokenizedEmailSchema}
340
- currentData={{ email: initialEmailAddress || '' }}
395
+ busy={saving}
396
+ definition={tokenizedEmailSchema}
397
+ initialValues={{ email: initialEmailAddress || '' }}
341
398
  onError={setError}
342
399
  onSubmit={onSubmit}
343
400
  buttonText={i18n.t('auth.resend_verification')}
344
401
  >
345
402
  {!error ? null : (
346
- <ModalErrorMessage error={error} resetErrors={() => setError(null)} />
403
+ <ModalErrorMessage
404
+ error={error}
405
+ resetErrors={() => setError(null)}
406
+ />
347
407
  )}
348
408
 
349
- <Markdown as="p" className={error ? 'py_xs a_c' : '-mt_5 pb_xs a_c'}>
409
+ <Markdown
410
+ as="p"
411
+ className={error ? 'py_xs a_c' : '-mt_5 pb_xs a_c'}
412
+ >
350
413
  {instructionText}
351
414
  </Markdown>
352
415
  </SchemaForm>
@@ -371,16 +434,9 @@ function ResetPasswordCompleted(p: AuthFormProps) {
371
434
  iconName="circle-check"
372
435
  >
373
436
  <TextWithLinks as="p">
374
- {i18n.t(
375
- emailAddress
376
- ? 'auth.reset_password_requested_msg'
377
- : 'auth.reset_password_requested_msg_no_email',
378
- { emailAddress },
379
- )}
380
- </TextWithLinks>
381
- <TextWithLinks as="p">
382
- {i18n.t('auth.if_need_more_help_email', { emailAddress: supportEmail })}
437
+ {i18n.t(emailAddress ? 'auth.reset_password_requested_msg' : 'auth.reset_password_requested_msg_no_email', { emailAddress })}
383
438
  </TextWithLinks>
439
+ <TextWithLinks as="p">{i18n.t('auth.if_need_more_help_email', { emailAddress: supportEmail })}</TextWithLinks>
384
440
  </ModalSimpleContent>
385
441
  );
386
442
  }
@@ -391,58 +447,58 @@ function ResetPasswordCompleted(p: AuthFormProps) {
391
447
 
392
448
  function SendPhoneVerification(p: AuthFormProps) {
393
449
  const { setAuthIface } = p;
394
- const sendPhoneVerificationSchema = useMemo(
395
- () => makeSendPhoneVerificationSchema(),
396
- [],
397
- );
450
+ const sendPhoneVerificationSchema = useMemo(() => makeSendPhoneVerificationSchema(), []);
398
451
 
399
452
  // Data hook
400
453
 
401
- const { sendPhoneVerificationCode, saving, error, setError, resetErrors } =
402
- useSendPhoneVerificationCode({
403
- onCompleted: (data: any, _: any, variables: any) => {
404
- const success = data?.sendPhoneVerificationCode;
405
- if (success) {
406
- setAuthIface(['VERIFY_PHONE_SENT', variables.phone]);
407
- }
408
- },
409
- onError: (err: any) => {
410
- setError(err);
411
- },
412
- });
454
+ const { sendPhoneVerificationCode, saving, error, setError, resetErrors } = useSendPhoneVerificationCode({
455
+ onCompleted: (data: any, _: any, variables: any) => {
456
+ const success = data?.sendPhoneVerificationCode;
457
+ if (success) {
458
+ setAuthIface(['VERIFY_PHONE_SENT', variables.phone]);
459
+ }
460
+ },
461
+ onError: (err: any) => {
462
+ setError(err);
463
+ },
464
+ });
413
465
 
414
- const onSubmit = (fv: any) => {
415
- sendPhoneVerificationCode({
466
+ /* Await the authentication request so submission state includes failures and completion. */
467
+ const onSubmit = async (fv: { phone: string }) => {
468
+ const result = await sendPhoneVerificationCode({
416
469
  variables: {
417
470
  phone: fv.phone,
418
471
  },
419
472
  });
473
+ return submissionResult(result);
420
474
  };
421
475
 
422
476
  return (
423
477
  <>
424
478
  <SchemaForm
425
479
  className="mb_df"
426
- saving={saving}
427
- schema={sendPhoneVerificationSchema}
480
+ busy={saving}
481
+ definition={sendPhoneVerificationSchema}
428
482
  onError={setError}
429
483
  onSubmit={onSubmit}
430
484
  buttonText={i18n.t('auth.send_verification_code')}
431
485
  >
432
486
  {!error ? null : (
433
- <ModalErrorMessage error={error} resetErrors={resetErrors} />
487
+ <ModalErrorMessage
488
+ error={error}
489
+ resetErrors={resetErrors}
490
+ />
434
491
  )}
435
492
 
436
- <p className={error ? 'py_xs a_c' : '-mt_5 pb_xs a_c'}>
437
- {i18n.t('auth.send_verification_code_msg')}
438
- </p>
493
+ <p className={error ? 'py_xs a_c' : '-mt_5 pb_xs a_c'}>{i18n.t('auth.send_verification_code_msg')}</p>
439
494
  </SchemaForm>
440
495
 
441
496
  <div className="a_c h_center">
442
497
  <button
498
+ type="button"
443
499
  disabled={saving}
444
500
  onClick={() => setAuthIface(null)}
445
- className="text ft_sm cl_md link"
501
+ className="text_link ft_sm cl_md link"
446
502
  >
447
503
  {i18n.t('auth.sign_in_other_methods')}
448
504
  </button>
@@ -460,34 +516,40 @@ interface ConfirmPhoneVerificationCodeProps extends AuthFormProps {
460
516
  }
461
517
 
462
518
  function ConfirmPhoneVerificationCode(p: ConfirmPhoneVerificationCodeProps) {
463
- const { setAuthIface, phoneNumber, authRoutesMap } = p;
519
+ const { setAuthIface, phoneNumber, authRoutesMap, popupId, popupMode, returnTo } = p;
464
520
  const [retryCount, setRetryCount] = useState(0);
465
521
  const navigate = useNavigate();
466
522
 
467
523
  // Data hook
468
524
 
469
- const { confirmPhoneVerificationCode, saving, error, setError, resetErrors } =
470
- useConfirmPhoneVerificationCode({
471
- onCompleted: async (data: any, _: any) => {
472
- const { success, token } = data?.confirmPhoneVerificationCode || {};
473
-
474
- if (success && token) {
475
- const persisted = await persistSignInSession(token);
476
- if (!persisted) {
477
- setError({
478
- message: i18n.t('error.network_error'),
479
- });
480
- return;
481
- }
482
- navigate(getAuthRoute('signin_success', authRoutesMap));
483
- } else {
484
- setRetryCount(retryCount + 1);
525
+ const { confirmPhoneVerificationCode, saving, error, setError, resetErrors } = useConfirmPhoneVerificationCode({
526
+ onCompleted: async (data: any, _: any) => {
527
+ const { success, token } = data?.confirmPhoneVerificationCode || {};
528
+
529
+ if (success && token) {
530
+ const persisted = await persistSignInSession(token);
531
+ if (!persisted) {
485
532
  setError({
486
- message: i18n.t('auth.verification_code_wrong'),
533
+ message: i18n.t('error.network_error'),
487
534
  });
535
+ return;
488
536
  }
489
- },
490
- });
537
+ navigate(
538
+ getSignInSuccessRoute({
539
+ authRoutesMap,
540
+ popupId,
541
+ popupMode,
542
+ returnTo,
543
+ }),
544
+ );
545
+ } else {
546
+ setRetryCount(retryCount + 1);
547
+ setError({
548
+ message: i18n.t('auth.verification_code_wrong'),
549
+ });
550
+ }
551
+ },
552
+ });
491
553
 
492
554
  const onSubmit = (code: string) => {
493
555
  confirmPhoneVerificationCode({
@@ -501,7 +563,10 @@ function ConfirmPhoneVerificationCode(p: ConfirmPhoneVerificationCodeProps) {
501
563
  return (
502
564
  <>
503
565
  {!error ? null : (
504
- <ModalErrorMessage error={error} resetErrors={resetErrors} />
566
+ <ModalErrorMessage
567
+ error={error}
568
+ resetErrors={resetErrors}
569
+ />
505
570
  )}
506
571
  <SMSCodeInput
507
572
  key={retryCount}
@@ -509,14 +574,13 @@ function ConfirmPhoneVerificationCode(p: ConfirmPhoneVerificationCodeProps) {
509
574
  error={error}
510
575
  onSubmit={onSubmit}
511
576
  />
512
- <p className="a_c p_n mt_md mb_df rel">
513
- {i18n.t('auth.confirm_verification_code_instr')}
514
- </p>
577
+ <p className="a_c p_n mt_md mb_df rel">{i18n.t('auth.confirm_verification_code_instr')}</p>
515
578
  <div className="a_c h_center">
516
579
  <button
580
+ type="button"
517
581
  disabled={saving}
518
582
  onClick={() => setAuthIface('VERIFY_PHONE')}
519
- className="text ft_sm cl_md link"
583
+ className="text_link ft_sm cl_md link"
520
584
  >
521
585
  {i18n.t('auth.resend_verification_code')}
522
586
  </button>
@@ -542,23 +606,27 @@ export function SignInForm(p: Omit<AuthFormProps, 'setAuthIface'>) {
542
606
  const { initialError } = p;
543
607
  const errCode = initialError?.errorCode;
544
608
  const initialIface =
545
- errCode === '20053'
546
- ? 'VERIFY_PHONE'
547
- : errCode === '20049'
548
- ? 'VERIFY_EMAIL'
549
- : null;
550
- const [signInIface_, setAuthIface] = useState<SignInIface | null>(
551
- initialIface,
552
- );
609
+ p.initialMethod === 'PHONE' ? 'VERIFY_PHONE' : errCode === '20053' ? 'VERIFY_PHONE' : errCode === '20049' ? 'VERIFY_EMAIL' : null;
610
+ const [signInIface_, setAuthIface] = useState<SignInIface | null>(initialIface);
553
611
  const ifaceIsArray = Array.isArray(signInIface_);
554
612
 
555
613
  switch (ifaceIsArray ? signInIface_[0] : signInIface_) {
556
614
  case 'SSO':
557
615
  // Email-first SSO sign in (org home realm discovery)
558
- return <SsoSignIn {...p} setAuthIface={setAuthIface} />;
616
+ return (
617
+ <SsoSignIn
618
+ {...p}
619
+ setAuthIface={setAuthIface}
620
+ />
621
+ );
559
622
  case 'VERIFY_PHONE':
560
623
  // Attempted phone login but it was not verified
561
- return <SendPhoneVerification {...p} setAuthIface={setAuthIface} />;
624
+ return (
625
+ <SendPhoneVerification
626
+ {...p}
627
+ setAuthIface={setAuthIface}
628
+ />
629
+ );
562
630
  case 'VERIFY_PHONE_SENT':
563
631
  // User requested phone verification code, and it was sent
564
632
  return (
@@ -586,11 +654,21 @@ export function SignInForm(p: Omit<AuthFormProps, 'setAuthIface'>) {
586
654
  />
587
655
  );
588
656
  case 'EMAIL_SENT':
589
- return <ResetPasswordCompleted {...p} setAuthIface={setAuthIface} />;
657
+ return (
658
+ <ResetPasswordCompleted
659
+ {...p}
660
+ setAuthIface={setAuthIface}
661
+ />
662
+ );
590
663
  default:
591
664
  }
592
665
 
593
- return <SignInMain {...p} setAuthIface={setAuthIface} />;
666
+ return (
667
+ <SignInMain
668
+ {...p}
669
+ setAuthIface={setAuthIface}
670
+ />
671
+ );
594
672
  }
595
673
 
596
674
  /**
@@ -603,52 +681,47 @@ interface SignUpMainProps extends Omit<AuthFormProps, 'setAuthIface'> {
603
681
  }
604
682
 
605
683
  function SignUpMain(p: SignUpMainProps) {
606
- const { authRoutesMap, initialError, setAuthIface, setSignUpToken } = p;
684
+ const { authRoutesMap, initialError, returnTo, setAuthIface, setSignUpToken } = p;
607
685
  const navigate = useNavigate();
608
686
  const signUpSchema = useMemo(makeSignUpSchema, []);
609
687
 
610
- const { saving, signUpWithEmail, error, setError, resetErrors } =
611
- useSignUpWithEmail({
612
- initialError,
613
- onCompleted: (data: any, _: any) => {
614
- const signUpToken = data?.signUpWithEmail;
688
+ const { saving, signUpWithEmail, error, setError, resetErrors } = useSignUpWithEmail({
689
+ initialError,
690
+ onCompleted: (data: any, _: any) => {
691
+ const signUpToken = data?.signUpWithEmail;
615
692
 
616
- if (signUpToken) {
617
- setSignUpToken(signUpToken);
618
- }
619
- },
620
- onError: (err: any) => {
621
- if (err?.errorCode === '20054') {
622
- setAuthIface('INVITATION_REQUIRED');
623
- }
624
- },
625
- });
693
+ if (signUpToken) {
694
+ setSignUpToken(signUpToken);
695
+ }
696
+ },
697
+ onError: (err: any) => {
698
+ if (err?.errorCode === '20054') {
699
+ setAuthIface('INVITATION_REQUIRED');
700
+ }
701
+ },
702
+ });
626
703
 
627
- const onSubmit = async (formValues: any) => {
628
- signUpWithEmail({
704
+ /* Await the authentication request so submission state includes failures and completion. */
705
+ const onSubmit = async (formValues: { email: string; password: string; passwordRepeat: string }) => {
706
+ const result = await signUpWithEmail({
629
707
  variables: {
630
708
  emailAddress: formValues.email,
631
709
  password: formValues.password,
632
710
  },
633
711
  });
712
+ return submissionResult(result);
634
713
  };
635
714
 
636
715
  // SSO providers auto-create accounts, so they work as sign up too
637
- const onFinishSignIn = async (
638
- token: string,
639
- sessionBootstrapToken?: string | null,
640
- sessionPersisted?: boolean,
641
- ) => {
642
- const persisted =
643
- sessionPersisted ||
644
- (await persistSignInSession(token, sessionBootstrapToken));
716
+ const onFinishSignIn = async (token: string, sessionBootstrapToken?: string | null, sessionPersisted?: boolean) => {
717
+ const persisted = sessionPersisted || (await persistSignInSession(token, sessionBootstrapToken));
645
718
  if (!persisted) {
646
719
  setError({
647
720
  message: i18n.t('error.network_error'),
648
721
  });
649
722
  return false;
650
723
  }
651
- navigate(getAuthRoute('signin_success', authRoutesMap));
724
+ navigate(getAuthRoute('signin_success', authRoutesMap, getAuthReturnQuery(returnTo)));
652
725
  return true;
653
726
  };
654
727
 
@@ -657,15 +730,17 @@ function SignUpMain(p: SignUpMainProps) {
657
730
  <ContinueWithGoogle onFinishSignIn={onFinishSignIn} />
658
731
  <ContinueWithMicrosoft onFinishSignIn={onFinishSignIn} />
659
732
 
660
- <FormBreak className="mt_md" colorClassName="strong_bf">
661
- {i18n.t('form.or')}
733
+ <FormBreak
734
+ className="mt_md"
735
+ colorClassName="strong_bf"
736
+ >
737
+ {i18n.t('c.or')}
662
738
  </FormBreak>
663
739
 
664
740
  <SchemaForm
665
741
  className="py_df"
666
- saving={saving}
667
- schema={signUpSchema}
668
- // currentData={{}}
742
+ busy={saving}
743
+ definition={signUpSchema}
669
744
  onError={setError}
670
745
  onSubmit={onSubmit}
671
746
  buttonText={i18n.t('auth.agree_and_continue')}
@@ -690,9 +765,9 @@ function SignUpMain(p: SignUpMainProps) {
690
765
 
691
766
  <div className="a_c h_center">
692
767
  <Link
693
- to={getAuthRoute('signin', authRoutesMap)}
768
+ to={getAuthRoute('signin', authRoutesMap, getAuthReturnQuery(returnTo))}
694
769
  replace
695
- className="text ft_sm cl_md link"
770
+ className="text_link ft_sm cl_md link"
696
771
  >
697
772
  {i18n.t('auth.sign_in_w_existing')}
698
773
  </Link>
@@ -705,7 +780,7 @@ function SignUpMain(p: SignUpMainProps) {
705
780
  * Sign up; invitation required
706
781
  */
707
782
 
708
- function SignUpInviteRequired(p: Omit<AuthFormProps, 'setAuthIface'>) {
783
+ function SignUpInviteRequired(_p: Omit<AuthFormProps, 'setAuthIface'>) {
709
784
  const supportEmail = APP_EMAILS.support;
710
785
 
711
786
  return (
@@ -713,9 +788,7 @@ function SignUpInviteRequired(p: Omit<AuthFormProps, 'setAuthIface'>) {
713
788
  title={i18n.t('auth.invite_required')}
714
789
  iconName="mailbox-filled"
715
790
  >
716
- <TextWithLinks as="p">
717
- {i18n.t('auth.invite_required_msg', { emailAddress: supportEmail })}
718
- </TextWithLinks>
791
+ <TextWithLinks as="p">{i18n.t('auth.invite_required_msg', { emailAddress: supportEmail })}</TextWithLinks>
719
792
  </ModalSimpleContent>
720
793
  );
721
794
  }
@@ -729,7 +802,7 @@ interface SignUpSuccessProps extends Omit<AuthFormProps, 'setAuthIface'> {
729
802
  }
730
803
 
731
804
  function SignUpSuccess(p: SignUpSuccessProps) {
732
- const { authRoutesMap } = p;
805
+ const { authRoutesMap, returnTo } = p;
733
806
 
734
807
  return (
735
808
  <ModalSimpleContent
@@ -740,7 +813,7 @@ function SignUpSuccess(p: SignUpSuccessProps) {
740
813
  <TextWithLinks as="p">{i18n.t('auth.account_created_msg')}</TextWithLinks>
741
814
 
742
815
  <FullWidthButton
743
- to={getAuthRoute('signin', authRoutesMap)}
816
+ to={getAuthRoute('signin', authRoutesMap, getAuthReturnQuery(returnTo))}
744
817
  preset="bg_primary"
745
818
  className="mt_lg shadow"
746
819
  replace
@@ -762,7 +835,12 @@ export function SignUpForm(p: Omit<AuthFormProps, 'setAuthIface'>) {
762
835
  const [signUpIface, setAuthIface] = useState<SignUpIface>(null);
763
836
 
764
837
  if (signUpToken) {
765
- return <SignUpSuccess {...p} signUpToken={signUpToken} />;
838
+ return (
839
+ <SignUpSuccess
840
+ {...p}
841
+ signUpToken={signUpToken}
842
+ />
843
+ );
766
844
  }
767
845
 
768
846
  switch (signUpIface) {