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