-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEmailVerifyBlock.ts
More file actions
179 lines (149 loc) · 5.3 KB
/
EmailVerifyBlock.ts
File metadata and controls
179 lines (149 loc) · 5.3 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import type {
AuthType,
CorbadoApp,
CorbadoError,
GeneralBlockVerifyIdentifier,
ProcessCommon,
} from '@corbado/web-core';
import type { Result } from 'ts-results';
import { Ok } from 'ts-results';
import { BlockTypes, ScreenNames } from '../constants';
import type { ErrorTranslator } from '../errorTranslator';
import type { ProcessHandler } from '../processHandler';
import type { BlockDataEmailVerify } from '../types';
import { Block } from './Block';
export class EmailVerifyBlock extends Block<BlockDataEmailVerify> {
readonly data: BlockDataEmailVerify;
readonly type = BlockTypes.EmailVerify;
readonly initialScreen;
readonly authType: AuthType;
readonly emailLinkToken?: string;
constructor(
app: CorbadoApp,
flowHandler: ProcessHandler,
common: ProcessCommon,
errorTranslator: ErrorTranslator,
data: GeneralBlockVerifyIdentifier,
authType: AuthType,
fromEmailVerifyFromUrl: boolean,
emailLinkToken?: string,
) {
super(app, flowHandler, common, errorTranslator);
switch (data.verificationMethod) {
case 'phone-otp':
throw new Error('SMS OTP verification is not supported for email verification');
case 'email-link':
if (fromEmailVerifyFromUrl) {
this.initialScreen = ScreenNames.EmailLinkVerification;
} else {
this.initialScreen = ScreenNames.EmailLinkSent;
}
break;
case 'email-otp':
this.initialScreen = ScreenNames.EmailOtpVerification;
break;
}
this.authType = authType;
this.emailLinkToken = emailLinkToken;
this.data = {
verificationMethod: data.verificationMethod,
email: data.identifier,
translatedError: errorTranslator.translate(data.error),
retryNotBefore: data.retryNotBefore,
isPostLoginVerification: data.isPostLoginVerification,
};
}
static fromBackend(
app: CorbadoApp,
flowHandler: ProcessHandler,
common: ProcessCommon,
translator: ErrorTranslator,
data: GeneralBlockVerifyIdentifier,
authType: AuthType,
): EmailVerifyBlock {
return new EmailVerifyBlock(app, flowHandler, common, translator, data, authType, false);
}
static fromUrl(
app: CorbadoApp,
flowHandler: ProcessHandler,
translator: ErrorTranslator,
data: GeneralBlockVerifyIdentifier,
authType: AuthType,
emailLinkToken: string,
): EmailVerifyBlock {
const emptyCommon: ProcessCommon = {
frontendApiUrl: '',
appName: '',
hideBadge: false,
environment: '',
};
return new EmailVerifyBlock(app, flowHandler, emptyCommon, translator, data, authType, true, emailLinkToken);
}
showEditEmail() {
this.data.translatedError = undefined;
this.updateScreen(ScreenNames.EditEmail);
}
showEmailVerificationScreen() {
this.data.translatedError = undefined;
if (this.data.verificationMethod === 'email-otp') {
this.updateScreen(ScreenNames.EmailOtpVerification);
} else {
this.updateScreen(ScreenNames.EmailLinkSent);
}
}
async validateCode(code: string) {
const processUpdate = await this.app.authProcessService.finishEmailCodeVerification(code);
this.updateProcess(processUpdate);
return;
}
async resendEmail() {
if (this.data.verificationMethod === 'email-otp') {
const newBlock = await this.app.authProcessService.startEmailCodeVerification();
this.updateProcess(newBlock);
} else {
const newBlock = await this.app.authProcessService.startEmailLinkVerification();
this.updateProcess(newBlock);
}
return;
}
async updateEmail(value: string): Promise<string | undefined> {
const newBlock = await this.app.authProcessService.updateEmail(value);
if (newBlock.err) {
this.updateProcess(newBlock);
return;
}
const error = newBlock.val.blockBody.error;
//If the new email is invalid, we don't want to update the block because the new block data from BE has no indicator for ScreenNames.EditEmail
//So, the FE needs to maintain state and we just want to show the translated error message
if (error) {
return this.errorTranslator.translateWithIdentifier(error, 'email');
}
await this.resendEmail();
this.showEmailVerificationScreen();
return;
}
async validateEmailLink(abortController: AbortController): Promise<Result<void, CorbadoError>> {
if (!this.emailLinkToken) {
throw new Error('Email link token is missing');
}
const res = await this.app.authProcessService.finishEmailLinkVerification(abortController, this.emailLinkToken);
this.updateProcess(res);
return Ok(void 0);
}
async getVerificationStatus(): Promise<Result<boolean, CorbadoError>> {
const newBlock = await this.app.authProcessService.getVerificationStatus();
if (newBlock.err) {
return newBlock;
}
// unlike all other requests blocks, this request's response is not always piped through updateProcess
// reason for that is that we don't want to refresh the whole screen after each poll (this would cause the counter to jump unpredictably due to network latency)
if (newBlock.val.blockBody.continueOnOtherDevice !== undefined) {
return Ok(true);
}
if (newBlock.val.blockBody.block === BlockTypes.EmailVerify) {
return Ok(false);
}
this.updateProcess(newBlock);
return Ok(false);
}
}