-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLoginHybridScreen.tsx
More file actions
86 lines (72 loc) · 3.16 KB
/
LoginHybridScreen.tsx
File metadata and controls
86 lines (72 loc) · 3.16 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
import type { ConnectError } from '@corbado/web-core';
import { ConnectErrorType } from '@corbado/web-core';
import type { ConnectLoginStartRsp } from '@corbado/web-core/dist/api/v2';
import log from 'loglevel';
import React, { useCallback, 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 LoginHybrid from './base/LoginHybrid';
import { connectLoginFinishToComplete, connectLoginFinishToWebauthnId } from './LoginInitScreen';
const LoginHybridScreen = (resStart: ConnectLoginStartRsp) => {
const { config, navigateToScreen, currentIdentifier, fallback } = useLoginProcess();
const [loading, setLoading] = useState(false);
const { getConnectService } = useShared();
const handleSubmit = useCallback(async () => {
if (loading) {
return;
}
setLoading(true);
const res = await getConnectService().loginContinue(resStart);
if (res.err) {
if (res.val.type === ConnectErrorType.Cancel || res.val.type === ConnectErrorType.Untyped) {
return handleSituation(LoginSituationCode.ClientPasskeyOperationCancelled, res.val);
}
return handleSituation(LoginSituationCode.CboApiNotAvailablePostAuthenticator, res.val);
}
try {
await config.onComplete(
connectLoginFinishToComplete(res.val),
getConnectService().encodeClientState(),
connectLoginFinishToWebauthnId(res.val),
);
} catch {
return handleSituation(LoginSituationCode.CtApiNotAvailablePostAuthenticator);
}
}, [getConnectService, config, navigateToScreen, currentIdentifier, loading]);
const handleSituation = (situationCode: LoginSituationCode, error?: ConnectError) => {
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.ClientPasskeyOperationCancelled:
navigateToScreen(LoginScreenType.ErrorSoft);
config.onError?.(situationCode.toString());
void getConnectService().recordEventLoginError(messageCode);
setLoading(false);
break;
case LoginSituationCode.ExplicitFallbackByUser:
navigateToScreen(LoginScreenType.Invisible);
fallback(identifier, null);
void getConnectService().recordEventLoginExplicitAbort(resStart.assertionOptions);
break;
}
};
return (
<LoginHybrid
loading={loading}
handleSubmit={() => void handleSubmit()}
handleFallback={() => handleSituation(LoginSituationCode.ExplicitFallbackByUser)}
/>
);
};
export default LoginHybridScreen;