Skip to content

Commit 221776f

Browse files
committed
registration: add function to re-request email token
1 parent 34cfa51 commit 221776f

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/interactive-auth.ts

+32-19
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ export class InteractiveAuth {
203203
private chosenFlow: IFlow = null;
204204
private currentStage: string = null;
205205

206+
private emailAttempt = 1;
207+
206208
// if we are currently trying to submit an auth dict (which includes polling)
207209
// the promise the will resolve/reject when it completes
208210
private submitPromise: Promise<void> = null;
@@ -408,6 +410,34 @@ export class InteractiveAuth {
408410
this.emailSid = sid;
409411
}
410412

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+
411441
/**
412442
* Fire off a request, and either resolve the promise, or call
413443
* startAuthStage.
@@ -458,24 +488,9 @@ export class InteractiveAuth {
458488
return;
459489
}
460490

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)) {
471492
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();
479494
// NB. promise is not resolved here - at some point, doRequest
480495
// will be called again and if the user has jumped through all
481496
// the hoops correctly, auth will be complete and the request
@@ -491,8 +506,6 @@ export class InteractiveAuth {
491506
// send the email, for whatever reason.
492507
this.attemptAuthDeferred.reject(e);
493508
this.attemptAuthDeferred = null;
494-
} finally {
495-
this.requestingEmailToken = false;
496509
}
497510
}
498511
}

0 commit comments

Comments
 (0)