visit
In this tutorial, we will be building a simple yet smart web application, to demonstrate how to authenticate users on a typical website or web application via face recognition instead of the traditional login/password pair.
In this process, we are going to rely on as the facial recognition library, React.js as the JavaScript framework, and TailwindCss as the CSS framework.
The final version of this project looks like this. Without further ado, let's get started.Please bear in-mind is that while this tutorial is focused on integrating FaceIO with React, the fio.js, facial recognition library can be integrated with any modern JavaScript framework out here including but not limited to React, Vue, Angular, Next or even Vanilla JavaScript. The reader is invited to take a look at the FaceIO or documentation for a step-by-step integration guide.
We are using vite to create the ReactJs project in this tutorial. Open
your terminal and run the following command to scaffold your React
project using vite.
npm create vite@latest my-project — — template react
Now follow the terminal prompt to scaffold your project. After you
completed this step, open this project in your favorite code editor. The
project structure should look like this.
├── index.html
├── package.json
├── package-lock.json
├── public
│ └── vite.svg
├── src
│ ├── App.css
│ ├── App.jsx
│ ├── assets
│ │ └── react.svg
│ └── main.jsx
└── vite.config.js
npm install -D tailwindcss postcss autoprefixer
Now open your
tailwind.config.js
file and add your project source path. /** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Now you have successfully installed tailwind CSS in your React and vite project. Let’s focus on how to integrate face authentication in our project in the next section all powered by .
The great thing about the fio.js is its straightforward usage. This library comes with only two major methods that are: enroll() and authenticate().
enroll() is for (registering) new users, while authenticate() is for previously enrolled users. Before discussing these methods, let’s link the fio.js library to our react application first. For that, just open the index.html file in your project and link to your project.
<body>
<script src="//cdn.faceio.net/fio.js"></script>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
import "./App.css";
import { useEffect } from "react";
function App(){
let faceio;
useEffect(() => {
faceio = new faceIO("your-app-id"); // Get yours from //console.faceio.net
}, []);
}
provides a very simple interface to register new users. We use enroll function to register a new user. As FaceIO needs your webcam permission, do accept the permission dialogue when prompted.
The enroll function has 2 aliases. You can use register and record functions as drop-in replacements for enroll.When you run this function in your browser, it launches the faceIO widget. At first, it takes user consent to scan their face, then prompt to accept
the webcam permission. If all requirements are satisfied, the faceIO
widget opens your camera and scans your face. It converts your face data
into an array of floating point numbers.
Let’s add faceIO enroll in our react application. We keep the app very simple. Therefore, let’s create a button called register when someone clicks on that button, we run the enroll function to enroll a new user.
After adding the register button and the javascript handler on the button click, App.jsx file looks like this.import "./App.css";
import { useEffect } from "react";
function App() {
let faceio;
useEffect(() => {
faceio = new faceIO("fioa414d");
}, []);
const handleRegister = async () => {
try {
let response = await faceio.enroll({
locale: "auto",
payload: {
email: "[email protected]",
},
});
console.log(` Unique Facial ID: ${response.facialId}
Enrollment Date: ${response.timestamp}
Gender: ${response.details.gender}
Age Approximation: ${response.details.age}`);
} catch (error) {
console.log(error);
}
};
return (
<section className="w-full h-screen flex flex-col items-center justify-center">
<h1 className="font-sans font-bold text-xl mb-4">
Face Authentication by FaceIO
</h1>
<div className="flex flex-col justify-center items-center">
<button
className="block px-4 py-2 outline-none bg-blue-500 rounded text-white mb-2"
onClick={handleRegister}
>
register
</button>
</div>
</section>
);
}
export default App;
The enroll function accepts only an optional parameter object. These
optional parameters accept properties to configure the user registration
process. In the above example, I define locale and payload properties inside the optional parameter of the enrolling function. the locale property represents the default interaction language of your user. In our case, we provide auto to automatically detect the language.
When the promise completes, it results in an userInfo object. By using the object, we can access the unique facial ID of the user. In addition to this, the userInfo object also provides a details property from which, we can get the probable age and gender of the user.
The authentication function needs only a single frame for the user to
recognize. Therefore, it is very bandwidth friendly. After successful
authentication, it returns a userData object. This userData contains the payload you have specifies in the registration and the face of the user.
Inside our App.jsx file, we make another button called Log-in. When a user clicks on this button, it invokes the handleLogIn helper function. This function is ultimately responsible to invoke the “authenticate” function. Let’s see all of these in code:
import "./App.css";
import { useEffect } from "react";
function App() {
let faceio;
useEffect(() => {
faceio = new faceIO("fioa414d");
}, []);
const handleRegister = async () => {
try {
let response = await faceio.enroll({
locale: "auto",
payload: {
email: "[email protected]",
},
});
console.log(` Unique Facial ID: ${response.facialId}
Enrollment Date: ${response.timestamp}
Gender: ${response.details.gender}
Age Approximation: ${response.details.age}`);
} catch (error) {
console.log(error);
}
};
return (
<section className="w-full h-screen flex flex-col items-center justify-center">
<h1 className="font-sans font-bold text-xl mb-4">
Face Authentication by FaceIO
</h1>
<div className="flex flex-col justify-center items-center">
<button
className="block px-4 py-2 outline-none bg-blue-500 rounded text-white mb-2"
onClick={handleRegister}
>
register
</button>
</div>
</section>
);
}
While creating your account in FaceIO, you will be assigned an API key. You
can find this API key in the faceIO console. By using this API key, you
can interact with the FaceIO back-end with . The base URL of the REST API is `//api.faceio.net/`.
To delete a given, we have to make a get request to endpoint. This endpoint has two request parameters. One is the key that accepts the API key, another is the fid which accepts the facial ID which you want to delete.
curl — request GET \— url '//api.faceio.net/deletefacialid?key=APIkey&fid=FaceID'
As a response, FaceIO returns a status code and a `payload` boolean that
tells if the data along with the payload is successfully deleted or not.
{
"eventName":"String - Event Name",
"facialId": "String - Unique Facial ID of the Target User",
"appId": "String - Application Public ID",
"clientIp": "String - Public IP Address",
"details": {
"timestamp": "Optional String - Event Timestamp",
"gender": "Optional String - Gender of the Enrolled User",
"age": "Optional String - Age of the Enrolled User"
}
}
This is an in-depth guide on adding facial authentication to your React
application. If you like this guide and want to know more about faceIO,
please visit the official . You can also checkout their if you have any questions.
Also published .