@@ -26,6 +26,7 @@ import type {
26
26
ResetPasswordParams ,
27
27
ResetPasswordPhoneCodeFactorConfig ,
28
28
SamlConfig ,
29
+ SetActiveNavigate ,
29
30
SignInCreateParams ,
30
31
SignInFirstFactor ,
31
32
SignInFutureResource ,
@@ -58,6 +59,7 @@ import {
58
59
webAuthnGetCredential as webAuthnGetCredentialOnWindow ,
59
60
} from '../../utils/passkeys' ;
60
61
import { createValidatePassword } from '../../utils/passwords/password' ;
62
+ import { runAsyncResourceTask } from '../../utils/runAsyncResourceTask' ;
61
63
import {
62
64
clerkInvalidFAPIResponse ,
63
65
clerkInvalidStrategy ,
@@ -493,8 +495,6 @@ class SignInFuture implements SignInFutureResource {
493
495
submitPassword : this . submitResetPassword . bind ( this ) ,
494
496
} ;
495
497
496
- fetchStatus : 'idle' | 'fetching' = 'idle' ;
497
-
498
498
constructor ( readonly resource : SignIn ) { }
499
499
500
500
get status ( ) {
@@ -506,8 +506,7 @@ class SignInFuture implements SignInFutureResource {
506
506
}
507
507
508
508
async sendResetPasswordEmailCode ( ) : Promise < { error : unknown } > {
509
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
510
- try {
509
+ return runAsyncResourceTask ( this . resource , async ( ) => {
511
510
if ( ! this . resource . id ) {
512
511
throw new Error ( 'Cannot reset password without a sign in.' ) ;
513
512
}
@@ -525,27 +524,16 @@ class SignInFuture implements SignInFutureResource {
525
524
body : { emailAddressId, strategy : 'reset_password_email_code' } ,
526
525
action : 'prepare_first_factor' ,
527
526
} ) ;
528
- } catch ( err : unknown ) {
529
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
530
- return { error : err } ;
531
- }
532
-
533
- return { error : null } ;
527
+ } ) ;
534
528
}
535
529
536
530
async verifyResetPasswordEmailCode ( { code } : { code : string } ) : Promise < { error : unknown } > {
537
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
538
- try {
531
+ return runAsyncResourceTask ( this . resource , async ( ) => {
539
532
await this . resource . __internal_basePost ( {
540
533
body : { code, strategy : 'reset_password_email_code' } ,
541
534
action : 'attempt_first_factor' ,
542
535
} ) ;
543
- } catch ( err : unknown ) {
544
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
545
- return { error : err } ;
546
- }
547
-
548
- return { error : null } ;
536
+ } ) ;
549
537
}
550
538
551
539
async submitResetPassword ( {
@@ -555,18 +543,12 @@ class SignInFuture implements SignInFutureResource {
555
543
password : string ;
556
544
signOutOfOtherSessions ?: boolean ;
557
545
} ) : Promise < { error : unknown } > {
558
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
559
- try {
546
+ return runAsyncResourceTask ( this . resource , async ( ) => {
560
547
await this . resource . __internal_basePost ( {
561
548
body : { password, signOutOfOtherSessions } ,
562
549
action : 'reset_password' ,
563
550
} ) ;
564
- } catch ( err : unknown ) {
565
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
566
- return { error : err } ;
567
- }
568
-
569
- return { error : null } ;
551
+ } ) ;
570
552
}
571
553
572
554
async create ( params : {
@@ -575,39 +557,26 @@ class SignInFuture implements SignInFutureResource {
575
557
redirectUrl ?: string ;
576
558
actionCompleteRedirectUrl ?: string ;
577
559
} ) : Promise < { error : unknown } > {
578
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
579
- try {
560
+ return runAsyncResourceTask ( this . resource , async ( ) => {
580
561
await this . resource . __internal_basePost ( {
581
562
path : this . resource . pathRoot ,
582
563
body : params ,
583
564
} ) ;
584
-
585
- return { error : null } ;
586
- } catch ( err : unknown ) {
587
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
588
- return { error : err } ;
589
- }
565
+ } ) ;
590
566
}
591
567
592
568
async password ( { identifier, password } : { identifier ?: string ; password : string } ) : Promise < { error : unknown } > {
593
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
594
- const previousIdentifier = this . resource . identifier ;
595
- try {
569
+ return runAsyncResourceTask ( this . resource , async ( ) => {
570
+ const previousIdentifier = this . resource . identifier ;
596
571
await this . resource . __internal_basePost ( {
597
572
path : this . resource . pathRoot ,
598
573
body : { identifier : identifier || previousIdentifier , password } ,
599
574
} ) ;
600
- } catch ( err : unknown ) {
601
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
602
- return { error : err } ;
603
- }
604
-
605
- return { error : null } ;
575
+ } ) ;
606
576
}
607
577
608
578
async sendEmailCode ( { email } : { email : string } ) : Promise < { error : unknown } > {
609
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
610
- try {
579
+ return runAsyncResourceTask ( this . resource , async ( ) => {
611
580
if ( ! this . resource . id ) {
612
581
await this . create ( { identifier : email } ) ;
613
582
}
@@ -623,27 +592,16 @@ class SignInFuture implements SignInFutureResource {
623
592
body : { emailAddressId, strategy : 'email_code' } ,
624
593
action : 'prepare_first_factor' ,
625
594
} ) ;
626
- } catch ( err : unknown ) {
627
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
628
- return { error : err } ;
629
- }
630
-
631
- return { error : null } ;
595
+ } ) ;
632
596
}
633
597
634
598
async verifyEmailCode ( { code } : { code : string } ) : Promise < { error : unknown } > {
635
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
636
- try {
599
+ return runAsyncResourceTask ( this . resource , async ( ) => {
637
600
await this . resource . __internal_basePost ( {
638
601
body : { code, strategy : 'email_code' } ,
639
602
action : 'attempt_first_factor' ,
640
603
} ) ;
641
- } catch ( err : unknown ) {
642
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
643
- return { error : err } ;
644
- }
645
-
646
- return { error : null } ;
604
+ } ) ;
647
605
}
648
606
649
607
async sso ( {
@@ -657,8 +615,7 @@ class SignInFuture implements SignInFutureResource {
657
615
redirectUrl : string ;
658
616
redirectUrlComplete : string ;
659
617
} ) : Promise < { error : unknown } > {
660
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
661
- try {
618
+ return runAsyncResourceTask ( this . resource , async ( ) => {
662
619
if ( flow !== 'auto' ) {
663
620
throw new Error ( 'modal flow is not supported yet' ) ;
664
621
}
@@ -678,27 +635,16 @@ class SignInFuture implements SignInFutureResource {
678
635
if ( status === 'unverified' && externalVerificationRedirectURL ) {
679
636
windowNavigate ( externalVerificationRedirectURL ) ;
680
637
}
681
- } catch ( err : unknown ) {
682
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
683
- return { error : err } ;
684
- }
685
-
686
- return { error : null } ;
638
+ } ) ;
687
639
}
688
640
689
- async finalize ( ) : Promise < { error : unknown } > {
690
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
691
- try {
641
+ async finalize ( { navigate } : { navigate ?: SetActiveNavigate } ) : Promise < { error : unknown } > {
642
+ return runAsyncResourceTask ( this . resource , async ( ) => {
692
643
if ( ! this . resource . createdSessionId ) {
693
644
throw new Error ( 'Cannot finalize sign-in without a created session.' ) ;
694
645
}
695
646
696
- await SignIn . clerk . setActive ( { session : this . resource . createdSessionId } ) ;
697
- } catch ( err : unknown ) {
698
- eventBus . emit ( 'resource:error' , { resource : this . resource , error : err } ) ;
699
- return { error : err } ;
700
- }
701
-
702
- return { error : null } ;
647
+ await SignIn . clerk . setActive ( { session : this . resource . createdSessionId , navigate } ) ;
648
+ } ) ;
703
649
}
704
650
}
0 commit comments