-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathBaseOnfidoWeb.tsx
156 lines (145 loc) · 6.27 KB
/
BaseOnfidoWeb.tsx
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
import {Onfido as OnfidoSDK} from 'onfido-sdk-ui';
import React, {forwardRef, useEffect} from 'react';
import type {ForwardedRef} from 'react';
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import Log from '@libs/Log';
import type {ThemeColors} from '@styles/theme/types';
import FontUtils from '@styles/utils/FontUtils';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import './index.css';
import type {OnfidoElement, OnfidoError, OnfidoProps} from './types';
type InitializeOnfidoProps = OnfidoProps &
Pick<LocaleContextProps, 'translate' | 'preferredLocale'> & {
theme: ThemeColors;
};
type OnfidoEvent = Event & {
detail?: Record<string, unknown>;
};
function initializeOnfido({sdkToken, onSuccess, onError, onUserExit, preferredLocale, translate, theme}: InitializeOnfidoProps) {
OnfidoSDK.init({
token: sdkToken,
containerId: CONST.ONFIDO.CONTAINER_ID,
customUI: {
fontFamilyTitle: `${FontUtils.fontFamily.platform.EXP_NEUE}, -apple-system, serif`,
fontFamilySubtitle: `${FontUtils.fontFamily.platform.EXP_NEUE}, -apple-system, serif`,
fontFamilyBody: `${FontUtils.fontFamily.platform.EXP_NEUE}, -apple-system, serif`,
fontSizeTitle: `${variables.fontSizeLarge}px`,
fontWeightTitle: Number(FontUtils.fontWeight.bold),
fontWeightSubtitle: 400,
fontSizeSubtitle: `${variables.fontSizeNormal}px`,
colorContentTitle: theme.text,
colorContentSubtitle: theme.text,
colorContentBody: theme.text,
borderRadiusButton: `${variables.buttonBorderRadius}px`,
colorBackgroundSurfaceModal: theme.appBG,
colorBorderDocTypeButton: theme.border,
colorBorderDocTypeButtonHover: theme.transparent,
colorBorderButtonPrimaryHover: theme.transparent,
colorBackgroundButtonPrimary: theme.success,
colorBackgroundButtonPrimaryHover: theme.successHover,
colorBackgroundButtonPrimaryActive: theme.successHover,
colorBorderButtonPrimary: theme.success,
colorContentButtonSecondaryText: theme.text,
colorBackgroundButtonSecondary: theme.border,
colorBackgroundButtonSecondaryHover: theme.icon,
colorBackgroundButtonSecondaryActive: theme.icon,
colorBorderButtonSecondary: theme.border,
colorBackgroundIcon: theme.transparent,
colorContentLinkTextHover: theme.appBG,
colorBorderLinkUnderline: theme.link,
colorBackgroundLinkHover: theme.link,
colorBackgroundLinkActive: theme.link,
colorBackgroundInfoPill: theme.link,
colorBackgroundSelector: theme.appBG,
colorBackgroundDocTypeButton: theme.success,
borderWidthSurfaceModal: '0px',
colorBackgroundDocTypeButtonHover: theme.successHover,
colorBackgroundButtonIconHover: theme.transparent,
colorBackgroundButtonIconActive: theme.transparent,
},
steps: [
{
type: CONST.ONFIDO.TYPE.DOCUMENT,
options: {
forceCrossDevice: true,
hideCountrySelection: true,
documentTypes: {
// eslint-disable-next-line @typescript-eslint/naming-convention
driving_licence: {
country: 'USA',
},
passport: true,
},
},
},
{
type: CONST.ONFIDO.TYPE.FACE,
options: {
requestedVariant: CONST.ONFIDO.VARIANT.VIDEO,
},
},
],
onComplete: (data) => {
if (isEmptyObject(data)) {
Log.warn('Onfido completed with no data');
}
onSuccess(data);
},
onError: (error: OnfidoError) => {
const errorType = error.type;
const errorMessage: string = error.message ?? CONST.ERROR.UNKNOWN_ERROR;
Log.hmmm('Onfido error', {errorType, errorMessage});
if (errorType === CONST.WALLET.ERROR.ONFIDO_USER_CONSENT_DENIED) {
onUserExit();
return;
}
onError(errorMessage);
},
language: {
// We need to use ES_ES as locale key because the key `ES` is not a valid config key for Onfido
locale: preferredLocale === CONST.LOCALES.ES ? CONST.LOCALES.ES_ES_ONFIDO : preferredLocale,
// Provide a custom phrase for the back button so that the first letter is capitalized,
// and translate the phrase while we're at it. See the issue and documentation for more context.
// https://github.com/Expensify/App/issues/17244
// https://documentation.onfido.com/sdk/web/#custom-languages
phrases: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'generic.back': translate('common.back'),
},
},
});
}
function logOnFidoEvent(event: OnfidoEvent) {
Log.hmmm('Receiving Onfido analytic event', event.detail);
}
function Onfido({sdkToken, onSuccess, onError, onUserExit}: OnfidoProps, ref: ForwardedRef<OnfidoElement>) {
const {preferredLocale, translate} = useLocalize();
const theme = useTheme();
useEffect(() => {
initializeOnfido({
sdkToken,
onSuccess,
onError,
onUserExit,
preferredLocale,
translate,
theme,
});
window.addEventListener('userAnalyticsEvent', logOnFidoEvent);
return () => window.removeEventListener('userAnalyticsEvent', logOnFidoEvent);
// Onfido should be initialized only once on mount
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
return (
<div
id={CONST.ONFIDO.CONTAINER_ID}
ref={ref}
/>
);
}
Onfido.displayName = 'Onfido';
export default forwardRef(Onfido);