Firebase Phone Authentication

You can use Firebase Authentication to sign in a user by sending an SMS message to the user's phone. The user signs in using a one-time code contained in the SMS message.

STEP 1: Enable Phone Number sign-in for your Firebase project. In the Firebase console, open the Authentication section. On the Sign-in Method page, enable the Phone Number sign-in method. On the same page, if the domain that will host your app isn't listed in the OAuth redirect domains section, add your domain. Firebase's phone number sign-in request quota is high enough that most apps won't be affected. However, if you need to sign in a very high volume of users with phone authentication, you might need to upgrade your pricing plan. See the pricing page.

STEP 2:Set up the reCAPTCHA verifier.


window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button', {
  'size': 'invisible',
  'callback': function(response) {    // reCAPTCHA solved, allow signInWithPhoneNumber.
    onSignInSubmit();
  }});

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


var phoneNumber = getPhoneNumberFromUserInput();var appVerifier = window.recaptchaVerifier;firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
    .then(function (confirmationResult) {      // SMS sent. Prompt user to type the code from the       // message, then sign the user in with       // confirmationResult.confirm(code).
      window.confirmationResult = confirmationResult;
    }).catch(function (error) {      // Error; SMS not sent      // ...
    });

STEP 4: Sign in the user with the verification code.


var code = getCodeFromUserInput();confirmationResult.confirm(code).then(function (result) {  // User signed in successfully.
  var user = result.user;  // ...}).catch(function (error) {  // User couldn't sign in (bad verification code?)  // ...});

var credential = firebase.auth.PhoneAuthProvider.
             credential(confirmationResult.verificationId, code);

firebase.auth().signInWithCredential(credential);

You can set up fictional phone numbers for development via the Firebase console. Testing with fictional phone numbers won't consume your usage quota.

Note that besides phone verification, Firebase also allows easy sign-in through ID providers such as Facebook, Google, Apple, Twitter, GitHub, Microsoft, Yahoo. This is commonly called Single Sign-On (SSO).