Repository navigation
Expand file tree
/
Copy pathflows.tsx
More file actions
1528 lines (1433 loc) · 39.9 KB
/
Copy pathflows.tsx
File metadata and controls
1528 lines (1433 loc) · 39.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* AuthKit flow components.
*
* Each flow is a small state machine that renders a card of inputs and
* reports its outcome to the parent App via `onDone`. Flows never hold
* global state — App.tsx owns step transitions + resulting session.
*/
import { useEffect, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import type { FormEvent } from 'react';
import type { AuthKitClient } from './api';
import type {
ApiError,
AuthFactor,
AuthMethod,
AuthResult,
EmailVerificationRequired,
InvitationLookup,
LoginSuccess,
MfaRequired,
Profile,
Step,
} from './types';
import { isEmailVerificationRequired, isMfaRequired } from './types';
import {
Alert,
Button,
Card,
Divider,
Field,
FlowCard,
Heading,
Input,
LinkButton,
Spinner,
Subheading,
} from './ui';
import {
AuthKitSlot,
SLOT_AFTER_FORM,
SLOT_AFTER_PRIMARY_ACTION,
SLOT_BEFORE_FORM,
SLOT_PICKER_AFTER_METHODS,
SLOT_PICKER_BEFORE_METHODS,
} from './slots';
import type { AuthKitSlotFillProps } from './slots';
function fillPropsFor(
profile: Profile,
step: Step,
flow?: string
): AuthKitSlotFillProps {
return {
step,
profileSlug: profile.slug,
methods: profile.methods || [],
flow,
};
}
function methodLabel( method: AuthMethod ): string {
switch ( method ) {
case 'oauth_google':
return __( 'Continue with Google', 'integration-workos' );
case 'oauth_microsoft':
return __( 'Continue with Microsoft', 'integration-workos' );
case 'oauth_github':
return __( 'Continue with GitHub', 'integration-workos' );
case 'oauth_apple':
return __( 'Continue with Apple', 'integration-workos' );
default:
return method;
}
}
function errorMessage( data: unknown ): string {
if ( ! data || typeof data !== 'object' ) {
return __( 'Something went wrong. Please try again.', 'integration-workos' );
}
const err = data as ApiError;
return err.message || err.code || __( 'Unexpected error.', 'integration-workos' );
}
// ---------------------------------------------------------------- MethodPicker
interface MethodPickerProps {
profile: Profile;
onChoose: ( step: Step ) => void;
onError: ( message: string ) => void;
}
export function MethodPicker( { profile, onChoose, onError }: MethodPickerProps ) {
const methods = profile.methods || [];
const hasPassword = methods.includes( 'password' );
const hasMagic = methods.includes( 'magic_code' );
const oauthMethods = methods.filter( ( m ) => m.startsWith( 'oauth_' ) );
const handleOAuth = async ( method: AuthMethod ): Promise< void > => {
try {
const response = await fetch(
`${ profile.restBaseUrl }/oauth/authorize-url?profile=${ encodeURIComponent(
profile.slug
) }&provider=${ encodeURIComponent( method ) }&redirect_to=${ encodeURIComponent(
profile.redirectTo || ''
) }`,
{ credentials: 'same-origin' }
);
const data = await response.json();
if ( response.ok && data.authorize_url ) {
window.location.assign( data.authorize_url );
} else {
onError( errorMessage( data ) );
}
} catch ( _ ) {
onError( __( 'Unable to start social sign-in.', 'integration-workos' ) );
}
};
const fillProps = fillPropsFor( profile, 'pick' );
const logoAlt =
profile.branding.heading || __( 'Sign in', 'integration-workos' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={ logoAlt }
fillProps={ fillProps }
header={
<>
<Heading>
{ profile.branding.heading ||
__( 'Sign in', 'integration-workos' ) }
</Heading>
{ profile.branding.subheading && (
<Subheading>{ profile.branding.subheading }</Subheading>
) }
</>
}
footer={
profile.signup?.enabled && ! profile.signup?.require_invite ? (
<p className="wa-footer">
{ __( 'New here?', 'integration-workos' ) }{ ' ' }
<LinkButton onClick={ () => onChoose( 'signup' ) }>
{ __( 'Create an account', 'integration-workos' ) }
</LinkButton>
</p>
) : null
}
>
<AuthKitSlot
name={ SLOT_PICKER_BEFORE_METHODS }
fillProps={ fillProps }
/>
{ oauthMethods.map( ( method ) => (
<Button
key={ method }
variant="secondary"
onClick={ () => handleOAuth( method ) }
>
{ methodLabel( method ) }
</Button>
) ) }
{ oauthMethods.length > 0 && ( hasPassword || hasMagic ) && (
<Divider>{ __( 'or', 'integration-workos' ) }</Divider>
) }
{ hasMagic && (
<Button onClick={ () => onChoose( 'magic_send' ) }>
{ __( 'Sign in with a code by email', 'integration-workos' ) }
</Button>
) }
{ hasPassword && (
<Button variant="secondary" onClick={ () => onChoose( 'password' ) }>
{ __( 'Sign in with password', 'integration-workos' ) }
</Button>
) }
<AuthKitSlot
name={ SLOT_PICKER_AFTER_METHODS }
fillProps={ fillProps }
/>
</FlowCard>
);
}
// -------------------------------------------------------------------- Password
interface PasswordProps {
client: AuthKitClient;
profile: Profile;
onMfa: ( data: MfaRequired ) => void;
onSuccess: ( data: LoginSuccess ) => void;
onEmailConfirmation: ( email: string ) => void;
onEmailVerification: ( data: EmailVerificationRequired ) => void;
onBack: ( target?: Step ) => void;
}
export function Password( { client, onMfa, onSuccess, onEmailConfirmation, onEmailVerification, onBack, profile }: PasswordProps ) {
const [ email, setEmail ] = useState( '' );
const [ password, setPassword ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ error, setError ] = useState( '' );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
const { ok, data } = await client.json< AuthResult >(
'/password/authenticate',
{ email, password, redirect_to: profile.redirectTo }
);
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
if ( isEmailVerificationRequired( data ) ) {
onEmailVerification( data );
return;
}
const result = data as AuthResult & { email_confirmation_required?: boolean };
if ( result.email_confirmation_required ) {
onEmailConfirmation( email );
return;
}
if ( isMfaRequired( result ) ) {
onMfa( result );
return;
}
onSuccess( result );
};
const fillProps = fillPropsFor( profile, 'password', 'password' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={ <Heading>{ __( 'Sign in', 'integration-workos' ) }</Heading> }
footer={
<p className="wa-footer">
<LinkButton onClick={ () => onBack() }>
{ __( '← Back', 'integration-workos' ) }
</LinkButton>
</p>
}
>
{ error && <Alert variant="error">{ error }</Alert> }
<form onSubmit={ submit }>
<AuthKitSlot name={ SLOT_BEFORE_FORM } fillProps={ fillProps } />
<Field label={ __( 'Email', 'integration-workos' ) } htmlFor="wa-email">
<Input
id="wa-email"
type="email"
value={ email }
onChange={ setEmail }
autoComplete="username"
autoFocus={ true }
required={ true }
/>
</Field>
<Field label={ __( 'Password', 'integration-workos' ) } htmlFor="wa-password">
<Input
id="wa-password"
type="password"
value={ password }
onChange={ setPassword }
autoComplete="current-password"
required={ true }
/>
</Field>
<AuthKitSlot name={ SLOT_AFTER_FORM } fillProps={ fillProps } />
<Button type="submit" disabled={ loading }>
{ loading ? <Spinner /> : __( 'Sign in', 'integration-workos' ) }
</Button>
<AuthKitSlot
name={ SLOT_AFTER_PRIMARY_ACTION }
fillProps={ fillProps }
/>
</form>
</FlowCard>
);
}
// ------------------------------------------------------------------ MagicSend
interface MagicSendProps {
client: AuthKitClient;
profile: Profile;
onCodeSent: ( email: string ) => void;
onBack: () => void;
}
export function MagicSend( { client, profile, onCodeSent, onBack }: MagicSendProps ) {
const [ email, setEmail ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ error, setError ] = useState( '' );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
const { ok, data } = await client.json( '/magic/send', { email } );
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
onCodeSent( email );
};
const fillProps = fillPropsFor( profile, 'magic_send', 'magic_send' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={
<>
<Heading>
{ __( 'Sign in with a code', 'integration-workos' ) }
</Heading>
<Subheading>
{ __(
'We’ll email you a short code to sign in.',
'integration-workos'
) }
</Subheading>
</>
}
footer={
<p className="wa-footer">
<LinkButton onClick={ onBack }>
{ __( '← Back', 'integration-workos' ) }
</LinkButton>
</p>
}
>
{ error && <Alert variant="error">{ error }</Alert> }
<form onSubmit={ submit }>
<AuthKitSlot name={ SLOT_BEFORE_FORM } fillProps={ fillProps } />
<Field label={ __( 'Email', 'integration-workos' ) } htmlFor="wa-email-magic">
<Input
id="wa-email-magic"
type="email"
value={ email }
onChange={ setEmail }
autoComplete="username"
autoFocus={ true }
required={ true }
/>
</Field>
<AuthKitSlot name={ SLOT_AFTER_FORM } fillProps={ fillProps } />
<Button type="submit" disabled={ loading }>
{ loading ? <Spinner /> : __( 'Send code', 'integration-workos' ) }
</Button>
<AuthKitSlot
name={ SLOT_AFTER_PRIMARY_ACTION }
fillProps={ fillProps }
/>
</form>
</FlowCard>
);
}
// ---------------------------------------------------------------- MagicVerify
interface MagicVerifyProps {
client: AuthKitClient;
profile: Profile;
email: string;
onMfa: ( data: MfaRequired ) => void;
onSuccess: ( data: LoginSuccess ) => void;
onBack: () => void;
}
export function MagicVerify( {
client,
email,
onMfa,
onSuccess,
onBack,
profile,
}: MagicVerifyProps ) {
const [ code, setCode ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ error, setError ] = useState( '' );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
const { ok, data } = await client.json< AuthResult >( '/magic/verify', {
email,
code,
redirect_to: profile.redirectTo,
} );
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
const result = data as AuthResult;
if ( isMfaRequired( result ) ) {
onMfa( result );
return;
}
onSuccess( result );
};
const fillProps = fillPropsFor( profile, 'magic_verify', 'magic_verify' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={
<>
<Heading>
{ __( 'Enter your code', 'integration-workos' ) }
</Heading>
<Subheading>
{ sprintf(
/* translators: %s: email address. */
__( 'We sent a code to %s.', 'integration-workos' ),
email
) }
</Subheading>
</>
}
footer={
<p className="wa-footer">
<LinkButton onClick={ onBack }>
{ __( '← Back', 'integration-workos' ) }
</LinkButton>
</p>
}
>
{ error && <Alert variant="error">{ error }</Alert> }
<form onSubmit={ submit }>
<AuthKitSlot name={ SLOT_BEFORE_FORM } fillProps={ fillProps } />
<Field label={ __( 'Code', 'integration-workos' ) } htmlFor="wa-code">
<Input
id="wa-code"
type="text"
value={ code }
onChange={ setCode }
autoComplete="one-time-code"
autoFocus={ true }
required={ true }
placeholder="123456"
/>
</Field>
<AuthKitSlot name={ SLOT_AFTER_FORM } fillProps={ fillProps } />
<Button type="submit" disabled={ loading }>
{ loading ? <Spinner /> : __( 'Sign in', 'integration-workos' ) }
</Button>
<AuthKitSlot
name={ SLOT_AFTER_PRIMARY_ACTION }
fillProps={ fillProps }
/>
</form>
</FlowCard>
);
}
// -------------------------------------------------------------- EmailVerify
interface EmailVerifyProps {
client: AuthKitClient;
profile: Profile;
email: string;
pendingAuthToken: string;
onMfa: ( data: MfaRequired ) => void;
onSuccess: ( data: LoginSuccess ) => void;
onBack: () => void;
}
/**
* Finish a password sign-in that WorkOS paused to verify the email. The
* code is already in the inbox; resending only needs the pending token,
* since the server remembers whose sign-in it is.
*/
export function EmailVerify( {
client,
email,
pendingAuthToken,
onMfa,
onSuccess,
onBack,
profile,
}: EmailVerifyProps ) {
const [ code, setCode ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ resending, setResending ] = useState( false );
const [ error, setError ] = useState( '' );
const [ notice, setNotice ] = useState( '' );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
setNotice( '' );
const { ok, data } = await client.json< AuthResult >(
'/email-verification/verify',
{
code,
pending_authentication_token: pendingAuthToken,
redirect_to: profile.redirectTo,
}
);
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
const result = data as AuthResult;
if ( isMfaRequired( result ) ) {
onMfa( result );
return;
}
onSuccess( result );
};
const resend = async () => {
if ( resending ) {
return;
}
setResending( true );
setError( '' );
setNotice( '' );
const { ok, data } = await client.json< { message?: string } >(
'/email-verification/resend',
{ pending_authentication_token: pendingAuthToken }
);
setResending( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
setNotice(
( data as { message?: string } ).message ||
__( 'A new code is on its way. Use the newest one.', 'integration-workos' )
);
};
const fillProps = fillPropsFor( profile, 'email_verify', 'email_verify' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={
<>
<Heading>
{ __( 'Verify your email', 'integration-workos' ) }
</Heading>
<Subheading>
{ sprintf(
/* translators: %s: email address. */
__(
'We sent a code to %s. Enter it to finish signing in.',
'integration-workos'
),
email
) }
</Subheading>
</>
}
footer={
<p className="wa-footer">
<LinkButton onClick={ onBack }>
{ __( '← Back', 'integration-workos' ) }
</LinkButton>
</p>
}
>
{ error && <Alert variant="error">{ error }</Alert> }
{ notice && <Alert variant="success">{ notice }</Alert> }
<form onSubmit={ submit }>
<AuthKitSlot name={ SLOT_BEFORE_FORM } fillProps={ fillProps } />
<Field label={ __( 'Code', 'integration-workos' ) } htmlFor="wa-code">
<Input
id="wa-code"
type="text"
value={ code }
onChange={ setCode }
autoComplete="one-time-code"
autoFocus={ true }
required={ true }
placeholder="123456"
/>
</Field>
<AuthKitSlot name={ SLOT_AFTER_FORM } fillProps={ fillProps } />
<Button type="submit" disabled={ loading }>
{ loading ? <Spinner /> : __( 'Sign in', 'integration-workos' ) }
</Button>
<AuthKitSlot
name={ SLOT_AFTER_PRIMARY_ACTION }
fillProps={ fillProps }
/>
</form>
<p className="wa-footer">
{ __( 'Didn’t get it?', 'integration-workos' ) }{ ' ' }
<LinkButton onClick={ resend }>
{ resending
? __( 'Sending…', 'integration-workos' )
: __( 'Send a new code', 'integration-workos' ) }
</LinkButton>
</p>
</FlowCard>
);
}
// ---------------------------------------------------------------------- Mfa
interface MfaChallengeProps {
client: AuthKitClient;
profile: Profile;
pendingAuthToken: string;
factors: AuthFactor[];
onSuccess: ( data: LoginSuccess ) => void;
}
export function MfaChallenge( {
client,
pendingAuthToken,
factors,
onSuccess,
profile,
}: MfaChallengeProps ) {
const firstFactor = factors && factors.length > 0 ? factors[ 0 ] : null;
const [ challengeId, setChallengeId ] = useState( '' );
const [ code, setCode ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ error, setError ] = useState( '' );
useEffect( () => {
if ( ! firstFactor ) {
return;
}
( async () => {
const { ok, data } = await client.json< { challenge_id: string } >(
'/mfa/challenge',
{ factor_id: firstFactor.id }
);
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
setChallengeId( ( data as { challenge_id: string } ).challenge_id );
} )();
}, [ firstFactor, client ] );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
const { ok, data } = await client.json< LoginSuccess >( '/mfa/verify', {
pending_authentication_token: pendingAuthToken,
authentication_challenge_id: challengeId,
code,
redirect_to: profile.redirectTo,
} );
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
onSuccess( data as LoginSuccess );
};
const fillProps = fillPropsFor( profile, 'mfa', 'mfa' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={
<>
<Heading>
{ __( 'Verify your identity', 'integration-workos' ) }
</Heading>
<Subheading>
{ firstFactor?.type === 'sms'
? __(
'Enter the code we just sent to your phone.',
'integration-workos'
)
: __(
'Enter the 6-digit code from your authenticator app.',
'integration-workos'
) }
</Subheading>
</>
}
>
{ error && <Alert variant="error">{ error }</Alert> }
<form onSubmit={ submit }>
<AuthKitSlot name={ SLOT_BEFORE_FORM } fillProps={ fillProps } />
<Field label={ __( 'Code', 'integration-workos' ) } htmlFor="wa-mfa-code">
<Input
id="wa-mfa-code"
type="text"
value={ code }
onChange={ setCode }
autoComplete="one-time-code"
autoFocus={ true }
required={ true }
placeholder="123456"
/>
</Field>
<AuthKitSlot name={ SLOT_AFTER_FORM } fillProps={ fillProps } />
<Button type="submit" disabled={ loading || ! challengeId }>
{ loading ? <Spinner /> : __( 'Verify', 'integration-workos' ) }
</Button>
<AuthKitSlot
name={ SLOT_AFTER_PRIMARY_ACTION }
fillProps={ fillProps }
/>
</form>
</FlowCard>
);
}
// -------------------------------------------------------------------- Signup
interface SignupContext {
userId: string;
email: string;
}
interface SignupProps {
client: AuthKitClient;
profile: Profile;
onVerify: ( ctx: SignupContext ) => void;
onBack: () => void;
}
export function Signup( { client, profile, onVerify, onBack }: SignupProps ) {
const [ email, setEmail ] = useState( '' );
const [ password, setPassword ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ error, setError ] = useState( '' );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
const { ok, data } = await client.json< {
verification_needed: boolean;
user: { id: string };
} >( '/signup/create', { email, password } );
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
const payload = data as { verification_needed: boolean; user: { id: string } };
if ( payload.verification_needed ) {
onVerify( { userId: payload.user.id, email } );
} else {
// Email already verified — send them to sign-in.
onBack();
}
};
const fillProps = fillPropsFor( profile, 'signup', 'signup' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={
<Heading>
{ __( 'Create your account', 'integration-workos' ) }
</Heading>
}
footer={
<p className="wa-footer">
<LinkButton onClick={ onBack }>
{ __(
'Already have an account? Sign in',
'integration-workos'
) }
</LinkButton>
</p>
}
>
{ error && <Alert variant="error">{ error }</Alert> }
<form onSubmit={ submit }>
<AuthKitSlot name={ SLOT_BEFORE_FORM } fillProps={ fillProps } />
<Field
label={ __( 'Email', 'integration-workos' ) }
htmlFor="wa-signup-email"
>
<Input
id="wa-signup-email"
type="email"
value={ email }
onChange={ setEmail }
autoComplete="email"
autoFocus={ true }
required={ true }
/>
</Field>
<Field
label={ __( 'Password', 'integration-workos' ) }
htmlFor="wa-signup-pw"
>
<Input
id="wa-signup-pw"
type="password"
value={ password }
onChange={ setPassword }
autoComplete="new-password"
required={ true }
/>
</Field>
<AuthKitSlot name={ SLOT_AFTER_FORM } fillProps={ fillProps } />
<Button type="submit" disabled={ loading }>
{ loading ? <Spinner /> : __( 'Create account', 'integration-workos' ) }
</Button>
<AuthKitSlot
name={ SLOT_AFTER_PRIMARY_ACTION }
fillProps={ fillProps }
/>
</form>
</FlowCard>
);
}
// ---------------------------------------------------------------- SignupVerify
interface SignupVerifyProps {
client: AuthKitClient;
profile: Profile;
userId: string;
email: string;
onDone: ( data: unknown ) => void;
}
export function SignupVerify( {
client,
profile,
userId,
email,
onDone,
}: SignupVerifyProps ) {
const [ code, setCode ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ error, setError ] = useState( '' );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
const { ok, data } = await client.json( '/signup/verify', {
user_id: userId,
code,
} );
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
onDone( data );
};
const fillProps = fillPropsFor( profile, 'signup_verify', 'signup_verify' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={
<>
<Heading>
{ __( 'Verify your email', 'integration-workos' ) }
</Heading>
<Subheading>
{ sprintf(
/* translators: %s: email address. */
__( 'Enter the code we sent to %s.', 'integration-workos' ),
email
) }
</Subheading>
</>
}
>
{ error && <Alert variant="error">{ error }</Alert> }
<form onSubmit={ submit }>
<AuthKitSlot name={ SLOT_BEFORE_FORM } fillProps={ fillProps } />
<Field label={ __( 'Code', 'integration-workos' ) } htmlFor="wa-verify">
<Input
id="wa-verify"
type="text"
value={ code }
onChange={ setCode }
autoFocus={ true }
required={ true }
placeholder="123456"
/>
</Field>
<AuthKitSlot name={ SLOT_AFTER_FORM } fillProps={ fillProps } />
<Button type="submit" disabled={ loading }>
{ loading ? <Spinner /> : __( 'Verify', 'integration-workos' ) }
</Button>
<AuthKitSlot
name={ SLOT_AFTER_PRIMARY_ACTION }
fillProps={ fillProps }
/>
</form>
</FlowCard>
);
}
// --------------------------------------------------------------------- Reset
interface ResetRequestProps {
client: AuthKitClient;
profile: Profile;
onSent: ( email: string ) => void;
onBack: () => void;
}
export function ResetRequest( { client, profile, onSent, onBack }: ResetRequestProps ) {
const [ email, setEmail ] = useState( '' );
const [ loading, setLoading ] = useState( false );
const [ error, setError ] = useState( '' );
const submit = async ( event: FormEvent ) => {
event.preventDefault();
setLoading( true );
setError( '' );
const { ok, data } = await client.json( '/password/reset/start', {
email,
redirect_url: profile.redirectTo,
} );
setLoading( false );
if ( ! ok ) {
setError( errorMessage( data ) );
return;
}
onSent( email );
};
const fillProps = fillPropsFor( profile, 'reset', 'reset' );
return (
<FlowCard
logoUrl={ profile.branding.logo_url }
logoAlt={
profile.branding.heading || __( 'Sign in', 'integration-workos' )
}
fillProps={ fillProps }
header={
<>
<Heading>
{ __( 'Reset your password', 'integration-workos' ) }
</Heading>
<Subheading>
{ __(
'Enter your email and we’ll send a reset link.',
'integration-workos'
) }
</Subheading>
</>
}
footer={
<p className="wa-footer">
<LinkButton onClick={ onBack }>
{ __( '← Back', 'integration-workos' ) }
</LinkButton>
</p>