-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLoginErrorScreenHard.tsx
More file actions
138 lines (114 loc) · 4.97 KB
/
LoginErrorScreenHard.tsx
File metadata and controls
138 lines (114 loc) · 4.97 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
import type { ConnectError } from '@corbado/web-core';
import { ConnectErrorType, PasskeyLoginSource } from '@corbado/web-core';
import log from 'loglevel';
import React, { useState } from 'react';
import useLoginProcess from '../../hooks/useLoginProcess';
import useShared from '../../hooks/useShared';
import { LoginScreenType } from '../../types/screenTypes';
import { getLoginErrorMessage, LoginSituationCode } from '../../types/situations';
import LoginErrorHard from './base/LoginErrorHard';
import {
type CboApiFallbackOperationError,
connectLoginFinishToComplete,
connectLoginFinishToWebauthnId,
} from './LoginInitScreen';
type Props = {
previousAssertionOptions: string;
};
const LoginErrorScreenHard = ({ previousAssertionOptions }: Props) => {
const { config, navigateToScreen, currentIdentifier, loadedMs, fallback } = useLoginProcess();
const { getConnectService } = useShared();
const [loading, setLoading] = useState(false);
const [hardErrorCount, setHardErrorCount] = useState(1);
// only for logging purposes
const [assertionOptions, setAssertionOptions] = useState<string | undefined>(previousAssertionOptions);
const handleSubmit = async () => {
if (loading) {
return;
}
setLoading(true);
const resStart = await getConnectService().loginStart(currentIdentifier, PasskeyLoginSource.ErrorHard, loadedMs);
if (resStart.err) {
return handleSituation(LoginSituationCode.CboApiNotAvailablePreAuthenticator, resStart.val);
}
if (resStart.val.assertionOptions.length === 0) {
const data: CboApiFallbackOperationError = {
initFallback: resStart.val.fallbackOperationError.initFallback,
identifierFallback: resStart.val.fallbackOperationError.identifier ?? '',
message: resStart.val.fallbackOperationError.error?.message ?? null,
};
return handleSituation(LoginSituationCode.CboApiFallbackOperationError, undefined, data);
}
setAssertionOptions(resStart.val.assertionOptions);
const resFinish = await getConnectService().loginContinue(resStart.val);
if (resFinish.err) {
if (resFinish.val.type === ConnectErrorType.Cancel) {
if (hardErrorCount >= 3) {
return handleSituation(LoginSituationCode.ClientPasskeyOperationCancelledTooManyTimes, resFinish.val);
}
return handleSituation(LoginSituationCode.ClientPasskeyOperationCancelled, resFinish.val);
}
return handleSituation(LoginSituationCode.CboApiNotAvailablePostAuthenticator, resFinish.val);
}
setLoading(false);
try {
await config.onComplete(
connectLoginFinishToComplete(resFinish.val),
getConnectService().encodeClientState(),
connectLoginFinishToWebauthnId(resFinish.val),
);
} catch {
return handleSituation(LoginSituationCode.CtApiNotAvailablePostAuthenticator);
}
};
const handleSituation = (situationCode: LoginSituationCode, error?: ConnectError, data?: unknown) => {
const messageCode = `situation: ${situationCode} ${error?.track()}`;
log.debug(messageCode);
const identifier = currentIdentifier;
const message = getLoginErrorMessage(situationCode);
switch (situationCode) {
case LoginSituationCode.CtApiNotAvailablePostAuthenticator:
case LoginSituationCode.CboApiNotAvailablePostAuthenticator:
navigateToScreen(LoginScreenType.Invisible);
fallback(identifier, message);
void getConnectService().recordEventLoginErrorUnexpected(messageCode);
setLoading(false);
break;
case LoginSituationCode.ClientPasskeyOperationCancelledTooManyTimes:
navigateToScreen(LoginScreenType.Invisible);
fallback(identifier, message);
void getConnectService().recordEventLoginError(messageCode);
setLoading(false);
break;
case LoginSituationCode.ClientPasskeyOperationCancelled:
setHardErrorCount(hardErrorCount + 1);
void getConnectService().recordEventLoginError(messageCode);
setLoading(false);
break;
case LoginSituationCode.ExplicitFallbackByUser:
navigateToScreen(LoginScreenType.Invisible);
fallback(identifier, null);
void getConnectService().recordEventLoginExplicitAbort(assertionOptions);
break;
case LoginSituationCode.CboApiFallbackOperationError: {
const { initFallback, identifierFallback, message } = data as CboApiFallbackOperationError;
if (initFallback) {
navigateToScreen(LoginScreenType.Invisible);
fallback(identifierFallback, message);
}
void getConnectService().recordEventLoginError(messageCode);
setLoading(false);
break;
}
}
};
return (
<LoginErrorHard
loading={loading}
handleSubmit={() => void handleSubmit()}
handleExplicitFallback={() => handleSituation(LoginSituationCode.ExplicitFallbackByUser)}
handleNeedHelp={config.onHelpClick ? () => config.onHelpClick?.() : undefined}
/>
);
};
export default LoginErrorScreenHard;