-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/AG-27144
- Loading branch information
Showing
42 changed files
with
724 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/background/emailConfirmationService/emailConfirmationService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { BrowserApi, browserApi } from '../browserApi'; | ||
import { RESEND_EMAIL_CONFIRMATION_CODE_DELAY_SEC } from '../../common/constants'; | ||
|
||
/** | ||
* Service for email confirmation. | ||
* | ||
* It is used to store confirmation authId and resend code count down. | ||
*/ | ||
class EmailConfirmationService { | ||
browserApi: BrowserApi; | ||
|
||
/** | ||
* Email for which count down is started. | ||
* | ||
* Needed to check if resend code count down should be restarted on popup navigation, | ||
* i.e. if user's email should be confirmed but user navigates back and changes the password — | ||
* count down should not be restarted in this case. | ||
*/ | ||
private email: string | null; | ||
|
||
/** | ||
* Value of 'auth_id' field from server response of registration or authorization request | ||
* if user should confirm email. | ||
*/ | ||
private confirmationAuthId: string | null; | ||
|
||
private COUNTDOWN_START_KEY = 'resend.code.countdown.start.time'; | ||
|
||
/** | ||
* Timestamp in milliseconds on which count down was started last time. | ||
*/ | ||
private countdownStartMs: number | null; | ||
|
||
constructor(providedBrowserApi: BrowserApi) { | ||
this.browserApi = providedBrowserApi; | ||
this.confirmationAuthId = null; | ||
} | ||
|
||
/** | ||
* Returns resend code count down start timestamp in MILLISECONDS. | ||
* | ||
* If there is no value for {@link countdownStartMs}, it will be fetched from storage. | ||
* | ||
* @returns Timestamp in milliseconds on which count down was started last time | ||
* or null if count down was not started. | ||
*/ | ||
getResentCodeCountdownStartFromStorage = async (): Promise<number | null> => { | ||
if (this.countdownStartMs) { | ||
return this.countdownStartMs; | ||
} | ||
this.countdownStartMs = await this.browserApi.storage.get(this.COUNTDOWN_START_KEY) || null; | ||
return this.countdownStartMs; | ||
}; | ||
|
||
/** | ||
* Sets resend code count down start timestamp in MILLISECONDS in storage. | ||
* | ||
* @param countdownStart Timestamp in milliseconds on which count down was started last time. | ||
*/ | ||
setResentCodeCountdownStartToStorage = async (countdownStart: number | null): Promise<void> => { | ||
this.countdownStartMs = countdownStart; | ||
await this.browserApi.storage.set(this.COUNTDOWN_START_KEY, countdownStart); | ||
}; | ||
|
||
/** | ||
* Resets count down and starts it again if the timer has not been started yet. | ||
* | ||
* @param email User email for which count down should be restarted. | ||
* If provided and is the same as previous, count down is not restarted. | ||
* If not provided, count down is restarted anyway. | ||
*/ | ||
public restartCountdown(email?: string): void { | ||
// if email is provided, check if it is the same as previous | ||
if (typeof email !== 'undefined') { | ||
if (this.email === email) { | ||
// do not restart count down if email is the same | ||
return; | ||
} | ||
// update email value | ||
this.email = email; | ||
} | ||
|
||
this.setResentCodeCountdownStartToStorage(Date.now()); | ||
} | ||
|
||
/** | ||
* Returns number of SECONDS left until user can request another code. | ||
* | ||
* @returns Number of seconds left or null if count down was not started or already finished. | ||
*/ | ||
public async getCodeCountdown(): Promise<number | null> { | ||
const countdownStartMs = await this.getResentCodeCountdownStartFromStorage(); | ||
if (!countdownStartMs) { | ||
return null; | ||
} | ||
|
||
// eslint-disable-next-line max-len | ||
const countdownSec = RESEND_EMAIL_CONFIRMATION_CODE_DELAY_SEC - Math.round((Date.now() - countdownStartMs) / 1000); | ||
return countdownSec > 0 | ||
? countdownSec | ||
: null; | ||
} | ||
|
||
/** | ||
* Sets authId which is needed for email confirmation, | ||
* i.e. for requesting another code. | ||
* | ||
* @param authId AuthId. | ||
*/ | ||
public setAuthId(authId: string) { | ||
this.confirmationAuthId = authId; | ||
} | ||
|
||
/** | ||
* Returns authId for confirmation. | ||
*/ | ||
public get authId(): string | null { | ||
return this.confirmationAuthId; | ||
} | ||
} | ||
|
||
export const emailConfirmationService = new EmailConfirmationService(browserApi); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { emailConfirmationService } from './emailConfirmationService'; |
Oops, something went wrong.