-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
133 lines (113 loc) · 4.28 KB
/
main.js
File metadata and controls
133 lines (113 loc) · 4.28 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
/* main.js */
/** Example Backend, this functions should be replaced with your actual backend implementation **/
import exampleBackend from "./example_backend";
let incodeSDKInstance;
let incodeSession;
let candidate;
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.- Start the authentication process
async function authenticate() {
const hintInput = document.getElementById("user-hint-input");
const userHintDiv = document.getElementById("user-hint-container");
userHintDiv.style.display = "none";
const identityId = hintInput.value;
incodeSession = await exampleBackend.start(identityId);
renderAuthentication(identityId);
}
// 2.- Render the face authentication
function renderAuthentication(hint = null) {
incodeSDKInstance.renderAuthFace(cameraContainer, {
session: incodeSession,
authHint: hint,
onSuccess: finishAuthentication, // Called after the SDK completes a successful face recognition.
onError: showError, // Called after the user runs out of capture attempts.
});
}
// 3.- Finish the onboarding
function finishAuthentication(response) {
console.log("Authentication successful:", response);
/** Example response
{
"overallStatus": "PASS",
"candidate": "68c851bedd5176f7d8bf4758",
"imageBase64": "/9j/4AAQSkZ...9843n4=",
"selfieEncryptedBase64": "/9j/4AAQSkZJ...RgAEj/9k=",
"metadata": "eyJjYXB0dXJ...NzF9f82f"
}
*/
candidate = response.candidate; // Store candidate globally
const container = document.getElementById("finish-container");
container.innerHTML = `
<h1>Authentication Process Finished</h1>
<p><strong>Candidate:</strong> ${candidate}</p>
<button id="verify-authentication-btn">Verify Authentication</button>
`;
document.getElementById("verify-authentication-btn").addEventListener("click", verifyAuthentication);
}
// 4.- Verify the authentication against the score
async function verifyAuthentication() {
console.log("Verifying authentication for candidate:", candidate);
try {
const validationResult = await exampleBackend.verifyAuthentication(
incodeSession.interviewId,
incodeSession.token,
candidate,
);
console.log("Validation result:", validationResult);
const container = document.getElementById("finish-container");
container.innerHTML += `
<hr>
<h2>Authentication Verification</h2>
<p><strong>Interview ID:</strong> ${incodeSession.interviewId}</p>
<p><strong>Candidate:</strong> ${candidate}</p>
<p><strong>Identity ID:</strong> ${validationResult.identityId || "N/A"}</p>
<p><strong>Message:</strong> ${validationResult.message}</p>
<p><strong>Authentication Valid:</strong> <span style="color: ${validationResult.valid ? "green" : "red"}; font-weight: bold;">${
validationResult.valid ? "✓ VALID" : "✗ INVALID"
}</span></p>
`;
document.getElementById("verify-authentication-btn").addEventListener("click", verifyAuthentication);
} catch (e) {
showError(e);
}
}
async function app() {
try {
// Translations if needed
// https://developer.incodeSDKInstance.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;
incodeSDKInstance = window.OnBoarding.create({
apiURL: apiURL,
//translations: en
});
await incodeSDKInstance.initialize();
// Create the single session
cameraContainer.innerHTML = "<h1>Creating session...</h1>";
// Empty the container and start the flow
cameraContainer.innerHTML = "";
} catch (e) {
showError(e);
}
}
document.addEventListener("DOMContentLoaded", app);
// Bind the continue button to getHint function
document.getElementById("continue-btn").addEventListener("click", authenticate);