Skip to content

Runtime Error: Firebase Phone Verification with Ionic or Cordova Never Works #1782

@ghost

Description

Version info

Angular: ionic-angular 3.9.2

AngularFire: 5.0.0-rc.11

Other (e.g. Ionic/Cordova, Node, browser, operating system):

(Cordova CLI) : 8.0.0

ionic (Ionic CLI) : 3.20.0

Android SDK Tools : 25.2.5
ios-deploy : 1.9.1
ios-sim : 5.0.8
Node : v8.11.3
npm : 6.1.0
OS : macOS High Sierra
Xcode : Xcode 9.4

How to reproduce these conditions

Git Repo : https://github.com/freelancersunil/firebase-phone-auth-ionic3-otp

Steps to set up and reproduce
Component HTML File:

<div id="recaptcha-container"></div>

  <ion-item>
    <ion-label stacked>Phone Number</ion-label>
    <ion-input type="number" [(ngModel)]="phoneNumber"></ion-input>
  </ion-item>
  
  <button ion-button id="sign-in-button" (click)="signIn(phoneNumber)">
    Sign In
  </button>

TS file:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, public afAuth: AngularFireAuth) {
  }

  afterViewInit() {
    this.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
  }

  signIn(phoneNumber: any, appVerifier: any) {
    this.afAuth.auth.signInWithPhoneNumber(phoneNumber, appVerifier)
      .then((confirmationResult) => {
        // SMS sent. Prompt user to type the code from the message, then sign the
        // user in with confirmationResult.confirm(code).
           let prompt = this.alertCtrl.create({
        title: 'Enter the Confirmation code',
        inputs: [{ name: 'confirmationCode', placeholder: 'Confirmation Code' }],
        buttons: [
          { text: 'Cancel',
            handler: data => { console.log('Cancel clicked'); }
          },
          { text: 'Send',
            handler: data => {
              confirmationResult.confirm(data.confirmationCode)
                .then(function (result) {
                  // User signed in successfully.
                  console.log(result.user);
                  // ...
                }).catch(function (error) {
                  // User couldn't sign in (bad verification code?)
                  // ...
                });
            }
          } 
        ]
      });
      prompt.present();
        console.log(confirmationResult);
      }).catch((error) => {
        // Error; SMS not sent
        // ...
      });
  }

  

}


Debug output

Runtime Error:
signInWithPhoneNumber failed: Second argument "applicationVerifier" must be an implementation of firebase.auth.ApplicationVerifier.

Typescript Error
Property 'recaptchaVerifier' does not exist on type 'LoginPage'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions