Firebase Phone Number Authentication



Firebase provides phone number authentication service for apple, android and web to help the to sign-up and log-in user using their phone number by sending them verification code (OTP). Follow the step the enable Phone Number Authentication for web.


1. Enable Phone Number sign-in for your Firebase project

  1. Open your project console and navigate to Build > Authentication dashboard in that open Sign-in method tab.
  2. Click on "Add new provider" and choose Phone that is in Native Provider column.
  3. Now enable Phone provider and hit Save button.
    phone provider
    Now you can use Phone Number to sign-in and authenticate user in firebase web. 
  4. If you haven't setup firebase SDK and initialize authentication then do it first.

2. Set up the reCAPTCHA verifier


You need to configure Firebase's reCAPTCHA verifier before allowing users to sign in with their phone numbers. Firebase uses reCAPTCHA to stop misuse, such as by making sure the request for a phone number verification comes from one of the permitted domains for your app.

So for that first create an div and assign it an id="recaptchaContainer" attribute.
<div id="recaptchaContainer"></div>

Hear we 
use invisible reCAPTCHA verifier and we need to set language so SMS can be sent in their native language.
import { auth } from "<YOUR_FIREBASE_CONFIG_FILE_PATH>"; //see setup and initialize authentication if you have any doubt
import { RecaptchaVerifier } from "firebase/auth";

function recaptchaGenerator() {
  auth.useDeviceLanguage(); //set device language
  return new RecaptchaVerifier(
    "recaptchaContainer",
    {
      size: "invisible",
    },
    auth,
  );
}

3. Send a verification code to the user's phone


Use this code to send OTP on user's phone number in firebase phone number authentication.
import { auth } from "<YOUR_FIREBASE_CONFIG_FILE_PATH>"; //see setup and initialize authentication if you have any doubt
import { signInWithPhoneNumber } from "firebase/auth";

const sendVerificationCode = async () => {
  try {
    const appVerifier = recaptchaGenerator(); // This is create for reCAPTCHA verifier [In STEP(2)]
    window.confirmationResult = await signInWithPhoneNumber(
      auth,
      "+10000000000", // Enter number along with country code
      appVerifier,
    );
  } catch (error) {
    console.log(error);
  }
};
Call sendVerificationCode function when you submit the number.

4. Sign in the user by verifying OTP code


This code perform the task to verify the entered OTP and help to sign in user phone authentication in firebase.
const verifyOTP = () => {
  window.confirmationResult
    .confirm("OTP_CODE")
    .then((result) => {
      const userData = result;
      // ... write code to perform intened task
      // ...
    })
    .catch((error) => console.log(error));
};
Call verifyOTP function when you submit the OTP to verify.

Related Post
Email/Password
How to Signup/ Login/ Logout User In Firebase Authentication ?
How to Signup/ Login/ Logout User In Firebase Authentication ?

1.Create New User Account: 'createUserWithEmailAndPassword()' method, 2.Login: 'signInWithEmailAndPassword()' method, 3.Logout: 'signOut(auth)' method

21 October 2024

Email link (Passwordless)
Authenticate User Using Email Link (Passwordless)
Authenticate User Using Email Link (Passwordless)

1. Enable Email Link sign-in method, 2. Send an authentication link to the user's email address, 3. Sign-In user

21 October 2024

Setup and Initialize
Setup and Initialize Firebase Authentication
Setup and Initialize Firebase Authentication

1.Add Firebase SDK, 2.Initialize app, 3.Iniitialize Authentication using getAuth(app)

21 October 2024

Google OAuth
Sign-In User using Google OAuth with Firebase
Sign-In User using Google OAuth with Firebase

Step 1. Enable Google sign-in method for your project, Step 2. Set up Sign-in flow.

21 October 2024

Phone number
Firebase Phone Number Authentication
Firebase Phone Number Authentication

1. Enable Phone Number sign-in for your Firebase project, 2. Set up the reCAPTCHA verifier, 3. Send a verification code to the user's phone, 4. Sign in the user by verifying OTP code

21 October 2024

Company
Explore

Copyright © 2024, All rights reserved by Firebase Master

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Allow", you consent to our use of cookies. Here is our Privacy Policy.