-
Notifications
You must be signed in to change notification settings - Fork 20
/
emailConfirmationService.ts
122 lines (107 loc) · 4.11 KB
/
emailConfirmationService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);