@@ -26,6 +26,7 @@ import type {
2626 ResetPasswordParams ,
2727 ResetPasswordPhoneCodeFactorConfig ,
2828 SamlConfig ,
29+ SetActiveNavigate ,
2930 SignInCreateParams ,
3031 SignInFirstFactor ,
3132 SignInFutureResource ,
@@ -58,6 +59,7 @@ import {
5859 webAuthnGetCredential as webAuthnGetCredentialOnWindow ,
5960} from '../../utils/passkeys' ;
6061import { createValidatePassword } from '../../utils/passwords/password' ;
62+ import { runAsyncResourceTask } from '../../utils/runAsyncResourceTask' ;
6163import {
6264 clerkInvalidFAPIResponse ,
6365 clerkInvalidStrategy ,
@@ -493,8 +495,6 @@ class SignInFuture implements SignInFutureResource {
493495 submitPassword : this . submitResetPassword . bind ( this ) ,
494496 } ;
495497
496- fetchStatus : 'idle' | 'fetching' = 'idle' ;
497-
498498 constructor ( readonly resource : SignIn ) { }
499499
500500 get status ( ) {
@@ -506,8 +506,7 @@ class SignInFuture implements SignInFutureResource {
506506 }
507507
508508 async sendResetPasswordEmailCode ( ) : Promise < { error : unknown } > {
509- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
510- try {
509+ return runAsyncResourceTask ( this . resource , async ( ) => {
511510 if ( ! this . resource . id ) {
512511 throw new Error ( 'Cannot reset password without a sign in.' ) ;
513512 }
@@ -525,27 +524,16 @@ class SignInFuture implements SignInFutureResource {
525524 body : { emailAddressId, strategy : 'reset_password_email_code' } ,
526525 action : 'prepare_first_factor' ,
527526 } ) ;
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+ } ) ;
534528 }
535529
536530 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 ( ) => {
539532 await this . resource . __internal_basePost ( {
540533 body : { code, strategy : 'reset_password_email_code' } ,
541534 action : 'attempt_first_factor' ,
542535 } ) ;
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+ } ) ;
549537 }
550538
551539 async submitResetPassword ( {
@@ -555,18 +543,12 @@ class SignInFuture implements SignInFutureResource {
555543 password : string ;
556544 signOutOfOtherSessions ?: boolean ;
557545 } ) : Promise < { error : unknown } > {
558- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
559- try {
546+ return runAsyncResourceTask ( this . resource , async ( ) => {
560547 await this . resource . __internal_basePost ( {
561548 body : { password, signOutOfOtherSessions } ,
562549 action : 'reset_password' ,
563550 } ) ;
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+ } ) ;
570552 }
571553
572554 async create ( params : {
@@ -575,39 +557,26 @@ class SignInFuture implements SignInFutureResource {
575557 redirectUrl ?: string ;
576558 actionCompleteRedirectUrl ?: string ;
577559 } ) : Promise < { error : unknown } > {
578- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
579- try {
560+ return runAsyncResourceTask ( this . resource , async ( ) => {
580561 await this . resource . __internal_basePost ( {
581562 path : this . resource . pathRoot ,
582563 body : params ,
583564 } ) ;
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+ } ) ;
590566 }
591567
592568 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 ;
596571 await this . resource . __internal_basePost ( {
597572 path : this . resource . pathRoot ,
598573 body : { identifier : identifier || previousIdentifier , password } ,
599574 } ) ;
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+ } ) ;
606576 }
607577
608578 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 ( ) => {
611580 if ( ! this . resource . id ) {
612581 await this . create ( { identifier : email } ) ;
613582 }
@@ -623,27 +592,16 @@ class SignInFuture implements SignInFutureResource {
623592 body : { emailAddressId, strategy : 'email_code' } ,
624593 action : 'prepare_first_factor' ,
625594 } ) ;
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+ } ) ;
632596 }
633597
634598 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 ( ) => {
637600 await this . resource . __internal_basePost ( {
638601 body : { code, strategy : 'email_code' } ,
639602 action : 'attempt_first_factor' ,
640603 } ) ;
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+ } ) ;
647605 }
648606
649607 async sso ( {
@@ -657,8 +615,7 @@ class SignInFuture implements SignInFutureResource {
657615 redirectUrl : string ;
658616 redirectUrlComplete : string ;
659617 } ) : Promise < { error : unknown } > {
660- eventBus . emit ( 'resource:error' , { resource : this . resource , error : null } ) ;
661- try {
618+ return runAsyncResourceTask ( this . resource , async ( ) => {
662619 if ( flow !== 'auto' ) {
663620 throw new Error ( 'modal flow is not supported yet' ) ;
664621 }
@@ -678,27 +635,16 @@ class SignInFuture implements SignInFutureResource {
678635 if ( status === 'unverified' && externalVerificationRedirectURL ) {
679636 windowNavigate ( externalVerificationRedirectURL ) ;
680637 }
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+ } ) ;
687639 }
688640
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 ( ) => {
692643 if ( ! this . resource . createdSessionId ) {
693644 throw new Error ( 'Cannot finalize sign-in without a created session.' ) ;
694645 }
695646
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+ } ) ;
703649 }
704650}
0 commit comments