-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAppendInitScreen.tsx
More file actions
303 lines (258 loc) · 10 KB
/
AppendInitScreen.tsx
File metadata and controls
303 lines (258 loc) · 10 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import type { ConnectError } from '@corbado/web-core';
import { ConnectErrorType } from '@corbado/web-core';
import type { AppendCompletionType } from '@corbado/web-core/dist/models/connect/append';
import log from 'loglevel';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import useAppendProcess from '../../hooks/useAppendProcess';
import useShared from '../../hooks/useShared';
import { Flags } from '../../types/flags';
import { AppendScreenType } from '../../types/screenTypes';
import { AppendSituationCode, getAppendErrorMessage } from '../../types/situations';
import { StatefulLoader } from '../../utils/statefulLoader';
import AppendBenefits from './append-init/AppendBenetifs';
import AppendInitLoaded2 from './append-init/AppendInitLoaded2';
import AppendInitLoading from './append-init/AppendInitLoading';
export enum AppendInitState {
SilentLoading,
Loading,
Loaded,
ShowBenefits,
}
const AppendInitScreen = () => {
const {
config,
navigateToScreen,
handleErrorHard,
handleErrorSoft,
handleSkip,
handleCredentialExistsError,
onReadMoreClick,
setFlags,
} = useAppendProcess();
const { sharedConfig, getConnectService } = useShared();
const [attestationOptions, setAttestationOptions] = useState('');
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
const [appendLoading, setAppendLoading] = useState(false);
const [appendInitState, setAppendInitState] = useState(AppendInitState.SilentLoading);
const [skipping, setSkipping] = useState(false);
const statefulLoader = useRef(
new StatefulLoader(
() => setAppendInitState(AppendInitState.Loading),
() => {
setAppendInitState(AppendInitState.Loaded);
},
() => {
setAppendInitState(AppendInitState.Loaded);
},
),
);
const simulateError = (): boolean => {
const urlParams = new URLSearchParams(window.location.search);
const maybeError = urlParams.get('cboSimulate');
if (!maybeError) {
return false;
}
// parse string to AppendSituationCode
const typed = AppendSituationCode[maybeError as keyof typeof AppendSituationCode];
void handleSituation(typed);
return true;
};
useEffect(() => {
const init = async (ac: AbortController) => {
if (simulateError()) {
return;
}
// get the time when the component is loaded (unix milliseconds)
const loadedMs = Date.now();
statefulLoader.current.start();
const url = new URL(window.location.href);
const invitationToken = url.searchParams.get('invitationToken');
if (invitationToken) {
getConnectService().setInvitation(invitationToken);
}
const res = await getConnectService().appendInit(ac);
if (res.err) {
if (res.val.type === ConnectErrorType.Cancel) {
return;
}
return handleSituation(AppendSituationCode.CboApiNotAvailablePreAuthenticator, res.val);
}
// we load flags from backend first, then we override them with the ones that are specified in the component's config
const flags = new Flags(res.val.flags);
if (sharedConfig.flags) {
flags.addFlags(sharedConfig.flags);
}
setFlags(flags);
if (!res.val.appendAllowed) {
return handleSituation(AppendSituationCode.DeniedByPartialRollout);
}
let appendToken: string;
try {
appendToken = await config.appendTokenProvider();
} catch {
return handleSituation(AppendSituationCode.CtApiNotAvailablePreAuthenticator);
}
const startAppendRes = await getConnectService().startAppend(appendToken, loadedMs, ac);
if (startAppendRes.err) {
if (startAppendRes.val.type === ConnectErrorType.Cancel) {
return;
}
return handleSituation(AppendSituationCode.CboApiNotAvailablePostAuthenticator, startAppendRes.val);
}
if (startAppendRes.val.attestationOptions === '') {
return handleSituation(AppendSituationCode.DeniedByPasskeyIntel);
}
if (startAppendRes.val.variant === 'after-hybrid') {
navigateToScreen(AppendScreenType.AfterHybridLogin, {
attestationOptions: startAppendRes.val.attestationOptions,
});
return;
}
if (startAppendRes.val.variant === 'after-error') {
navigateToScreen(AppendScreenType.AfterError, { attestationOptions: startAppendRes.val.attestationOptions });
return;
}
setAttestationOptions(startAppendRes.val.attestationOptions);
log.debug('startAppendRes', startAppendRes, flags);
if (startAppendRes.val.conditionalAppend) {
log.debug('starting conditional create');
const handledByConditionalCreate = await handleConditionalCreate(startAppendRes.val.attestationOptions);
log.debug('handledByConditionalCreate', handledByConditionalCreate);
if (handledByConditionalCreate) {
statefulLoader.current.finish();
return;
}
}
statefulLoader.current.finish();
if (startAppendRes.val.autoAppend || flags.hasSupportForAutomaticAppend()) {
console.log('starting auto-append');
await handleSubmit(startAppendRes.val.attestationOptions, 'auto');
}
};
log.debug('init AppendInitScreen');
const abortController = new AbortController();
init(abortController).catch(e => {
log.error(`init error: ${e}`);
});
return () => {
abortController.abort();
};
}, []);
const handleSubmit = useCallback(
async (attestationOptions: string, completionType: AppendCompletionType) => {
if (appendLoading || skipping) {
return;
}
setAppendLoading(true);
setErrorMessage(undefined);
const res = await getConnectService().completeAppend(attestationOptions, completionType);
if (res.err) {
if (res.val.type === ConnectErrorType.ExcludeCredentialsMatch) {
return handleSituation(AppendSituationCode.ClientExcludeCredentialsMatch, res.val);
}
if (res.val.type === ConnectErrorType.Cancel) {
if (completionType === 'auto') {
return handleSituation(AppendSituationCode.ClientPasskeyOperationCancelledSilent, res.val);
} else {
return handleSituation(AppendSituationCode.ClientPasskeyOperationCancelled, res.val);
}
}
return handleSituation(AppendSituationCode.CboApiNotAvailablePostAuthenticator, res.val);
}
setAppendLoading(false);
navigateToScreen(AppendScreenType.Success, {
aaguidName: res.val.passkeyOperation.aaguidDetails?.name,
aaguidIcon: res.val.passkeyOperation.aaguidDetails?.iconLight,
});
},
[getConnectService, appendLoading, skipping],
);
const handleConditionalCreate = useCallback(
async (attestationOptions: string) => {
const res = await getConnectService().completeAppend(attestationOptions, 'conditional');
if (res.err) {
await handleSituation(AppendSituationCode.ClientPasskeyOperationErrorSilent, res.val);
return res.val.type === ConnectErrorType.RaceTimeout;
}
navigateToScreen(AppendScreenType.Success, {
aaguidName: res.val.passkeyOperation.aaguidDetails?.name,
aaguidIcon: res.val.passkeyOperation.aaguidDetails?.iconLight,
});
return true;
},
[getConnectService],
);
const handleSituation = async (situationCode: AppendSituationCode, error?: ConnectError) => {
log.debug(`situation: ${situationCode}`);
const message = getAppendErrorMessage(situationCode);
if (message) {
setErrorMessage(message);
}
switch (situationCode) {
case AppendSituationCode.CtApiNotAvailablePreAuthenticator:
case AppendSituationCode.CboApiNotAvailablePreAuthenticator:
case AppendSituationCode.CboApiNotAvailablePostAuthenticator:
void handleErrorHard(situationCode, false, error);
statefulLoader.current.finishWithError();
break;
case AppendSituationCode.ClientPasskeyOperationCancelled:
void handleErrorSoft(situationCode, true, true, error);
setAppendLoading(false);
break;
case AppendSituationCode.ClientPasskeyOperationCancelledSilent:
void handleErrorSoft(situationCode, true, false, error);
setAppendLoading(false);
break;
case AppendSituationCode.ClientExcludeCredentialsMatch:
void handleCredentialExistsError(error);
setAppendLoading(false);
break;
case AppendSituationCode.DeniedByPartialRollout:
case AppendSituationCode.DeniedByPasskeyIntel:
await handleSkip(situationCode, false);
break;
case AppendSituationCode.ExplicitSkipByUser:
await handleSkip(situationCode, true);
break;
case AppendSituationCode.ClientPasskeyOperationErrorSilent:
void handleErrorSoft(situationCode, false, false, error);
setAppendLoading(false);
break;
}
};
const onSkip = useCallback(() => {
if (skipping || appendLoading) {
return;
}
setSkipping(true);
void handleSituation(AppendSituationCode.ExplicitSkipByUser);
}, [skipping, appendLoading]);
switch (appendInitState) {
case AppendInitState.SilentLoading:
return <></>;
case AppendInitState.Loading:
return <AppendInitLoading />;
case AppendInitState.ShowBenefits:
return <AppendBenefits onClick={() => setAppendInitState(AppendInitState.Loaded)} />;
case AppendInitState.Loaded:
return (
<AppendInitLoaded2
errorMessage={errorMessage}
appendLoading={appendLoading}
handleShowBenefits={() => {
void onReadMoreClick();
setAppendInitState(AppendInitState.ShowBenefits);
}}
handleSubmit={() => {
let completionType: AppendCompletionType = 'manual';
if (errorMessage) {
completionType = 'manual-retry';
}
void handleSubmit(attestationOptions, completionType);
}}
handleSkip={() => onSkip()}
/>
);
}
};
export default AppendInitScreen;