DEV Community

Pius oruko
Pius oruko

Posted on

Building Face Authentication in React with FaceIO and Bootstrap 5

face authentication
Web authentication has evolved significantly over time, moving away from traditional login/password pairs towards more secure and convenient methods. One of the latest advancements in web authentication is facial authentication, which provides a seamless and secure way to authenticate users using their face instead of passwords or OTP codes. In this article, we will explore how to build face authentication in a React application using the FaceIO library and Bootstrap 5. With FaceIO, we can easily integrate facial recognition into our web application, offering a passwordless experience to users. Let's dive into the implementation details and create a robust and user-friendly face authentication system in React.
FACEIO features: Secure, Convenient, and Privacy-Focused
Image description
FACEIO provides instant user authentication without the need for additional keys or codes. With defense-grade accuracy and fast recognition speeds, it offers reliable facial recognition powered by advanced engines. Its independence from external dependencies and cross-browser compatibility make it accessible and cost-effective on any device. Utilizing facial recognition helps reduce fraud and enables passwordless authentication for improved conversion rates. FACEIO prioritizes user privacy, following strict policies and procedures. It offers flexible deployment options, including cloud and on-premise solutions, ensuring complete control over user data. With built-in privacy tools, it allows for user opt-out and deletion while safeguarding data privacy.
Step 1: Create a React project with Bootstrap
Open your terminal and run the following command to scaffold your React project using Create React App with Bootstrap template:

npx create-react-app my-project
cd my-project
npm install bootstrap@5.0.0
Enter fullscreen mode Exit fullscreen mode

Step 2: Install FaceIO library
Create a FaceIO account and get the required credentials, such as the FaceIO CDN link and public ID, from the FaceIO console.
Open the index.html file in your project and add the FaceIO CDN link:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>FaceIO Authentication</title>
  <!-- Add Bootstrap CSS -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css">
  <!-- Add FaceIO CDN -->
  <script src="https://cdn.face.io/faceio.js"></script>
</head>
<body>
  <div id="root"></div>
  <script src="./src/index.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a React component for authentication
Replace the contents of src/App.js with the following code:

import React, { useEffect } from 'react';

const App = () => {
  useEffect(() => {
    const initFaceIO = async () => {
      try {
        const faceIO = new FaceIO('YOUR_FACEIO_ID');
        await faceIO.init();
        console.log('FaceIO initialized successfully');
      } catch (error) {
        console.error('Failed to initialize FaceIO:', error);
      }
    };
    initFaceIO();
  }, []);

  const handleRegister = async () => {
    try {
      const faceIO = new FaceIO('YOUR_FACEIO_ID');
      const enrollOptions = {
        locale: 'auto',
        payload: { email: 'user@example.com' }
      };
      const userInfo = await faceIO.enroll(enrollOptions);
      console.log('User registered successfully:', userInfo);
    } catch (error) {
      console.error('Failed to register user:', error);
    }
  };

  const handleLogin = async () => {
    try {
      const faceIO = new FaceIO('YOUR_FACEIO_ID');
      const userData = await faceIO.authenticate();
      console.log('User authenticated successfully:', userData);
    } catch (error) {
      console.error('Failed to authenticate user:', error);
    }
  };

  return (
    <div className="container">
      <h1 className="my-4">FaceIO Authentication</h1>
      <div className="d-grid gap-2 col-6 mx-auto my-4">
        <button className="btn btn-primary" onClick={handleRegister}>
          Register
        </button>
        <button className="btn btn-success" onClick={handleLogin}>
          Log In
        </button>
      </div>
    </div>
  );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

Step 4: Style the application using Bootstrap
Open the src/index.js file and replace its contents with the following code:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

import 'bootstrap/dist/css/bootstrap.min.css';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
Enter fullscreen mode Exit fullscreen mode

Step 5: Start the development server
Save all the changes and start the development server:
npm start
Now, you can open your browser and access the application at http://localhost:3000. The page will display buttons for user registration and login. When clicked, the respective functions will be triggered, calling FaceIO functions to perform enrollment and authentication.
Remember to replace 'YOUR_FACEIO_ID' in the code with your actual FaceIO ID obtained from the FaceIO console.
Note: This tutorial assumes you have the necessary FaceIO library and credentials. Please refer to the official FaceIO documentation for more details on the library and its usage.
Managing FACEIO application after implementation

- Addressing security

FACEIO offers a range of core security features to enhance the security of your applications. These features include rejecting weak PIN codes during enrollment, preventing duplicate enrollments, detecting face spoofing attempts, forbidding minors from enrolling, asking for PIN codes during authentication, enforcing PIN code uniqueness, handling obscured and partially masked faces, and verifying HTTP request headers. Additional security measures include restricting widget instantiation to authorized domain names and countries, enabling webhooks for real-time event notifications, and configuring specific security settings through the Application Manager. By leveraging these features, developers can ensure a robust and secure face authentication experience for their users.

- Privacy handling

Privacy by Design is a core principle in FACEIO, ensuring privacy is prioritized throughout the development process. The platform includes tools for privacy protection, such as access controls, user consent during enrollment, and easy opt-out options. When implementing FACEIO, it is crucial to communicate privacy practices and offer features like Unique Facial ID access and deletion. Responsibility lies with the application owner to safeguard and manage Unique Facial IDs, complying with privacy best practices and legal requirements like GDPR. Meaningful consent is essential, allowing users to be aware, have freedom of choice, maintain control, and understand the purpose and assurances of facial recognition technology. FACEIO recommends obtaining appropriate consent, providing clear notifications, and enabling users to revoke consent and delete their data. Additional recommendations include avoiding auto-enrollment, notifying users of biometric hash use, understanding legal requirements, providing policies and disclosures, following data retention policies, implementing data security measures, and conducting regular security audits. By adhering to these recommendations, application owners can create a privacy-centric and secure facial recognition experience.
Rest API endpoints

URL access endpoints in FACEIO play a crucial role in interacting with the FACEIO API and managing various aspects of your FACEIO application. These endpoints allow you to perform specific actions, retrieve information, and control the behavior of your facial recognition system. Here's an explanation of the role of each URL access endpoint:
**

  • DELETEFACIALID Endpoint:
    **
    Role: This endpoint enables you to programmatically delete (purge) a Facial ID, its linked payload data, and associated biometrics hash from your FACEIO application. It helps you remove user-specific data from your system.
    Access URL: https://api.faceio.net/deletefacialid

  • CHECKFACIALID Endpoint:
    Role: The CHECKFACIALID endpoint allows you to perform an authenticity check of a given Facial ID. It verifies whether the supplied Facial ID is valid and registered in your FACEIO application.
    Access URL: https://api.faceio.net/checkfacialid
    **

  • SETFACIALIDPAYLOAD Endpoint:
    **
    Role: This endpoint enables you to set or update payload data linked to a particular enrolled user on your FACEIO application via their Facial ID. The payload data is forwarded back to you upon successful authentication of that user in the future.
    Access URL: https://api.faceio.net/setfacialidpayload
    **

  • SETFACIALIDPINCODE Endpoint:
    **
    Role: The SETFACIALIDPINCODE endpoint allows you to set or change the PIN code linked to a specific enrolled user on your FACEIO application via their Facial ID. The PIN code is used for facial authentication and can be updated as needed.
    Access URL: https://api.faceio.net/stat
    In conclusion, implementing face authentication in a React application using FaceIO and Bootstrap 5 offers a secure and convenient method for user authentication. With FaceIO, developers can integrate facial recognition with defense-grade accuracy and fast recognition speeds. The platform prioritizes user privacy, follows privacy-by-design principles, and provides tools for privacy protection and consent management. Addressing security is crucial, and FACEIO offers core security features to enhance application security. The REST API endpoints in FACEIO play a crucial role in managing the facial recognition system. By considering these aspects and following implementation details, developers can create a user-friendly and secure face authentication system in React.

Top comments (0)