@@ -203,6 +203,8 @@ export class InteractiveAuth {
203
203
private chosenFlow : IFlow = null ;
204
204
private currentStage : string = null ;
205
205
206
+ private emailAttempt = 1 ;
207
+
206
208
// if we are currently trying to submit an auth dict (which includes polling)
207
209
// the promise the will resolve/reject when it completes
208
210
private submitPromise : Promise < void > = null ;
@@ -408,6 +410,34 @@ export class InteractiveAuth {
408
410
this . emailSid = sid ;
409
411
}
410
412
413
+ /**
414
+ * Requests a new email token and sets the email sid for the validation session
415
+ */
416
+ public requestEmailToken = async ( ) => {
417
+ if ( ! this . requestingEmailToken ) {
418
+ logger . trace ( "Requesting email token. Attempt: " + this . emailAttempt ) ;
419
+ // If we've picked a flow with email auth, we send the email
420
+ // now because we want the request to fail as soon as possible
421
+ // if the email address is not valid (ie. already taken or not
422
+ // registered, depending on what the operation is).
423
+ this . requestingEmailToken = true ;
424
+ try {
425
+ const requestTokenResult = await this . requestEmailTokenCallback (
426
+ this . inputs . emailAddress ,
427
+ this . clientSecret ,
428
+ this . emailAttempt ++ ,
429
+ this . data . session ,
430
+ ) ;
431
+ this . emailSid = requestTokenResult . sid ;
432
+ logger . trace ( "Email token request succeeded" ) ;
433
+ } finally {
434
+ this . requestingEmailToken = false ;
435
+ }
436
+ } else {
437
+ logger . warn ( "Could not request email token: Already requesting" ) ;
438
+ }
439
+ } ;
440
+
411
441
/**
412
442
* Fire off a request, and either resolve the promise, or call
413
443
* startAuthStage.
@@ -458,24 +488,9 @@ export class InteractiveAuth {
458
488
return ;
459
489
}
460
490
461
- if (
462
- ! this . emailSid &&
463
- ! this . requestingEmailToken &&
464
- this . chosenFlow . stages . includes ( AuthType . Email )
465
- ) {
466
- // If we've picked a flow with email auth, we send the email
467
- // now because we want the request to fail as soon as possible
468
- // if the email address is not valid (ie. already taken or not
469
- // registered, depending on what the operation is).
470
- this . requestingEmailToken = true ;
491
+ if ( ! this . emailSid && this . chosenFlow . stages . includes ( AuthType . Email ) ) {
471
492
try {
472
- const requestTokenResult = await this . requestEmailTokenCallback (
473
- this . inputs . emailAddress ,
474
- this . clientSecret ,
475
- 1 , // TODO: Multiple send attempts?
476
- this . data . session ,
477
- ) ;
478
- this . emailSid = requestTokenResult . sid ;
493
+ await this . requestEmailToken ( ) ;
479
494
// NB. promise is not resolved here - at some point, doRequest
480
495
// will be called again and if the user has jumped through all
481
496
// the hoops correctly, auth will be complete and the request
@@ -491,8 +506,6 @@ export class InteractiveAuth {
491
506
// send the email, for whatever reason.
492
507
this . attemptAuthDeferred . reject ( e ) ;
493
508
this . attemptAuthDeferred = null ;
494
- } finally {
495
- this . requestingEmailToken = false ;
496
509
}
497
510
}
498
511
}
0 commit comments