Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Firebase invisible reCAPTCHA for angular web app?

Hoping to see a working example. Searched quite a bit, can't seem to get it working locally. Always shows privacy terms logo & user has to interact with captcha.

Here is a codepen I've created to speed things up.

From the Firebase-Docs:

Use invisible reCAPTCHA

To use an invisible reCAPTCHA, create a RecaptchaVerifier object with the size parameter set to invisible, specifying the ID of the button that submits your sign-in form.

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

Basically what I have in this views on init:

window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('phone-sign-in-recaptcha', {
  'size': 'invisible',
  'callback': function(response) {
    // reCAPTCHA solved - will proceed with submit function
    console.log(response);
  },
  'expired-callback': function() {
    // Reset reCAPTCHA?
  }
});
<form name="mobile" novalidate>
  <label class="item item-input">
    <i class="icon placeholder-icon"></i>
    <input type="tel" name="number" ng-model="$ctrl.user.mobile">
  </label>
  <button id="sign-in-button" ng-click="$ctrl.onSignInSubmit(user.mobile)"></button>
</form>
<div id="phone-sign-in-recaptcha"></div>

This is the function called on submit:

ctrl.onSignInSubmit = function() {
  var phoneNumber = '+1' + ctrl.user.mobile;
  var appVerifier = window.recaptchaVerifier;

  firebase.auth()
    .signInWithPhoneNumber(phoneNumber, appVerifier)
    .then(function(confirmationResult) {
      ctrl.hasCodeToVerify = true;
      console.log('confirmationResult', confirmationResult);
      window.confirmationResult = confirmationResult;
    }).catch(function(error) {
      console.log('error', error);
    });
};
like image 227
alphapilgrim Avatar asked Oct 22 '17 19:10

alphapilgrim


1 Answers

Here is a working example with invisible reCAPTCHA:

https://github.com/firebase/quickstart-js/blob/master/auth/phone-invisible.html

In your code, after you call signInWithPhoneNumber, invisible reCAPTCHA should either display a challenge which when solved, resolves the pending promise with the confirmationResult, or directly resolves without showing any challenge.

When that promise resolves with confirmationResult, you should ask the user for the SMS code. You then call confirmationResult.confirm(code) This will complete sign in of the user.

firebase
  .auth()
  .signInWithPhoneNumber(phoneNumber, appVerifier)
  .then(function(confirmationResult) {
    var code = window.prompt("Please enter your code");
    return confirmationResult.confirm(code);
  })
  .then(function(result) {
    // User is signed in now.
  })
  .catch(function(error) {
    console.log("error", error);
  });
like image 76
bojeil Avatar answered Oct 09 '22 15:10

bojeil