-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
191 lines (174 loc) · 5.3 KB
/
main.js
File metadata and controls
191 lines (174 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
180
181
182
183
184
185
186
187
188
189
190
191
import { fakeBackendStart, fakeBackendFinish } from "./fake_backend";
const consentId = import.meta.env.VITE_CONSENT_ID;
// Simple configuration flag for V2 components
const forceV2 = false; // Control whether to force V2 experience for components
// const UiConfig = {
// branding: {
// logo: { src: "https://incode.com/wp-content/uploads/2024/03/incode-logo.svg", height: "50px" },
// hideFooterBranding: true,
// },
// theming: {
// designTokens: {
// colors: {
// brand: {
// 500: "#007aff",
// },
// positive: {
// 500: "#5ba66e",
// },
// negative: {
// 500: "#b80046",
// },
// },
// button: {
// primary: {
// surface: { default: "#007aff", hover: "#0056b3" },
// text: { default: "#ffffff" },
// },
// },
// typography: {
// fontSize: { base: "16px", lg: "1.125rem" },
// fontWeight: { medium: "300", bold: "700" },
// },
// },
// },
// closeButton: { show: true, position: "right" },
// };
let incode;
let incodeSession;
const cameraContainer = document.getElementById("camera-container");
function showError(e = null) {
const finishContainer = document.getElementById("finish-container");
if (e?.message) {
finishContainer.innerHTML = `<h1>Error: ${e.message}</h1>`;
} else {
finishContainer.innerHTML = "<h1>There was an error</h1>";
console.log(e);
}
}
// 1.- Check if mandatory consent is required and show it if it is.
function checkMandatoryConsent() {
incode.sendFingerprint({ token: incodeSession.token }).then((response) => {
// Send fingerprint returns a response with the following structure:
// {
// "success": true,
// "sessionStatus": "Alive",
// "ipCountry": "UNITED STATES",
// "ipState": "ILLINOIS",
// "showMandatoryConsent": true,
// "regulationType": "US_Illinois"
// }
// If the response has showMandatoryConsent and is set to true, we need to show the mandatory consent
if (response?.showMandatoryConsent) {
incode.renderBiometricConsent(cameraContainer, {
token: incodeSession,
onSuccess: captureCombinedConsent,
onCancel: () => showError("Mandatory consent was denied"),
regulationType: response.regulationType,
});
} else {
captureCombinedConsent();
}
});
}
// 2.- Show the combined consent
// This consent is created in the dashboard and has to be passed as a parameter
// to the renderCombinedConsent function.
function captureCombinedConsent() {
incode.renderCombinedConsent(cameraContainer, {
token: incodeSession,
onSuccess: sendGeolocation,
consentId: consentId, // id of a consent created in dashboard
});
}
// 3.- Send geolocation and start the ID capture flow
function sendGeolocation() {
incode.sendGeolocation({ token: incodeSession.token });
captureId();
}
// 4.- Capture the ID
function captureId() {
incode.renderCaptureId(cameraContainer, {
session: incodeSession,
onSuccess: processId,
onError: showError,
forceIdV2: forceV2, // Use the global V2 flag
//uiConfig: UiConfig, // Uncomment to apply custom theming
});
}
// 5.- Process the ID
async function processId() {
try {
const results = await incode.processId({ token: incodeSession.token });
console.log("processId results", results);
captureSelfie();
} catch (error) {
showError(error);
}
}
// 6.- Capture the selfie
function captureSelfie() {
incode.renderCaptureFace(cameraContainer, {
session: incodeSession,
onSuccess: processFace,
onError: showError,
forceV2: forceV2, // Use the global V2 flag
//uiConfig: UiConfig, // Uncomment to apply custom theming
});
}
// 7.- Process the Face
async function processFace() {
try {
const results = await incode.processFace({
token: incodeSession.token,
});
console.log("processFace results", results);
finishOnboarding();
} catch (error) {
showError(error);
}
}
// 8.- Finish the onboarding
function finishOnboarding() {
fakeBackendFinish(incodeSession.token)
.then((response) => {
console.log(response);
const container = document.getElementById("finish-container");
container.innerHTML = "<h1>Onboarding Finished.</h1>";
})
.catch((e) => {
showError(e);
});
}
async function app() {
try {
// Translations if needed
// https://developer.incode.com/docs/localization-and-strings#example-translations-files
// const en = {
// common: {
// continue: "Continue",
// error: "Error",
// loading: "Loading...",
// pleaseWait: "Please wait...",
// processing: "Processing...",
// // ...
// },
// };
const apiURL = import.meta.env.VITE_API_URL;
incode = window.OnBoarding.create({
apiURL: apiURL,
//translations: en
});
await incode.initialize();
// Create the single session
cameraContainer.innerHTML = "<h1>Creating session...</h1>";
incodeSession = await fakeBackendStart();
// Empty the container and start the flow
cameraContainer.innerHTML = "";
// Start the onboarding flow with mandatory consent
checkMandatoryConsent();
} catch (e) {
showError(e);
}
}
document.addEventListener("DOMContentLoaded", app);