Phone number authentication is an excellent way to allow users to sign in without remembering passwords. It also enhances security by ensuring that users have access to a real, active phone number.

In this guide, we'll break down every piece of code, making it super easy for beginners to understand.

Why Phone Authentication Over Email/Password?

  1. More Secure: A phone number is harder to hack than a simple email/password combination.

  2. User Convenience: Users don’t have to remember passwords.

  3. Better Accessibility: Many users prefer phone authentication, especially in regions where emails are less commonly used.

  4. Reduces Fake Accounts: Phone authentication ensures each user has a real, unique number.

Why Use FirebaseUI?

FirebaseUI simplifies authentication by handling UI rendering, user sign-in flows, and error handling. Instead of writing complex logic for handling OTPs, FirebaseUI does it all with minimal setup. It supports multiple sign-in providers and ensures a smooth user experience.

Step 1: Setting Up Firebase in Your React App

Before we jump into authentication, we need to set up Firebase.

Getting Your Firebase Credentials

  1. Go to Firebase Console.
  2. Create a new project or select an existing one.
  3. Navigate to Project Settings → General.
  4. Scroll down to Your Apps and register a new app (choose Web).
  5. Copy your Firebase configuration and replace it in the code below.

Installing Firebase

Make sure you have Firebase installed in your project. If not, run:

npm install firebase

The utils/firebase.js file

This file is responsible for initializing Firebase and making its services accessible throughout our app.

Code:

Breaking it Down:

  1. Imports Firebase: We import Firebase and the necessary modules like authentication, Firestore, storage, and functions.
  2. Firebase Configuration: Replace placeholders with your actual Firebase credentials.
  3. Initialize Firebase: firebase.initializeApp(firebaseConfig); sets up Firebase with our config.
  4. Export Firebase Services:
    • auth: Used for authentication.
    • db: Firestore database for storing data.
    • storage: For handling file uploads.
    • functions: Cloud functions for serverless operations.
  5. For the purpose of this article, we‘ll only be making use of the auth and firebase objects.

Step 2: Setting Up FirebaseUI for Phone Authentication

To make phone authentication easier, we’ll use FirebaseUI.

Installing FirebaseUI

npm install firebaseui

Importing and Configuring FirebaseUI

Code:

Breaking it Down:

  1. Imports FirebaseUI: import * as firebaseui from 'firebaseui';
  2. Imports Firebase Auth: import { auth } from './utils/services';
  3. Defines uiConfig:
    • signInOptions: Specifies phone authentication as a sign-in method.
    • recaptchaParameters: Adds reCAPTCHA verification for security.
    • defaultCountry: Sets the default country for phone authentication.
  4. Callback Function: Runs after a successful login.
  5. Creates FirebaseUI Instance: const ui = new firebaseui.auth.AuthUI(auth); // creates a new instance of FirebaseUI if does not already exist.

Step 3: Integrating FirebaseUI into a React Component

Code:

Breaking it Down:

  1. Imports Dependencies
  2. Uses useRef: This ensures we only initialize FirebaseUI once.
  3. useEffect Hook:
    • Initializes FirebaseUI.
    • Starts the authentication UI inside #firebaseui-auth-container.
  4. Renders FirebaseUI Container

Step 4: Using the PhoneAuth Component

Now, we can use the PhoneAuth component in our app:

Step 5: Deploying and Testing

  1. Run Your App

npm start

  1. Test with Your Phone Number

You should see outputs similar to the below screenshots:

Conclusion

We've successfully implemented phone authentication in React using Firebase and FirebaseUI. Now, users can sign in using their phone numbers without passwords. 🚀

Need more improvements? Let me know in the comments! 🎉

Additional Improvements

You can always reach me via: