Saturday, 6 October 2018

Ionic firebase phone auth plugin not installed

I am using this following plugin for phone auth on ionic: https://github.com/sajTempler/IonicFirebasePhoneAuth, im using my country code which is 55 and my operator number which is 15. As far as i was debugging there is nothing wrong with my code. Whenever i run it on my real device my console gets logged registerPhone err plugin_not_installed, i have no idea which plugin it refers to.

here's the code

    import {Component, ViewChild, OnInit} from '@angular/core';
import {IonicPage, NavController, AlertController} from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {Firebase} from '@ionic-native/firebase';
import * as firebase from 'firebase';

/**
 * 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 implements OnInit {

  @ViewChild('phoneNumber') phoneNumber;

  constructor(
    private navCtrl: NavController,
    private fireAuth: AngularFireAuth,
    private alertCtrl: AlertController,
    private fire: Firebase,
  ) {
  }

  ngOnInit() {
    console.log('LoginPage ngOnInit');
    this.fireAuth.authState.subscribe(auth => {
      if (!auth) {
        return;
      }

      auth.getIdToken()
        .then((token: string) => {
          console.log('LoginPage getIdToken token', token);
          if (token) {
            this.doLogin();
          }
        });
    });
  }

  // tslint:disable-next-line
  private registerPhone(): void {
    console.log('registerPhone');
    const phone = '+5515' + this.phoneNumber.value;
    console.log('registerPhone phone', phone);
    this.fire.verifyPhoneNumber(phone, 120)
      .then((res) => {
        const {verificationId} = res;
        console.log('registerPhone verificationId', verificationId);
        alert(verificationId);
        this.showPrompt(verificationId);
      })
      .catch(err => {
        console.log('registerPhone err', err);
      })
  }

  private async verifyCode(code: string, verificationId: string) {
    try {
      const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code);
      await firebase.auth().signInWithCredential(credential)
        .then(() => {
          this.doLogin();
        })
        .catch(err => {
          console.error('LoginPage verifyCode signInWithCredential err', err);
        })
    } catch (err) {
      console.error('LoginPage verifyCode err', err);
    }
  }

  private showPrompt(verificationId: string) {
    let promptCode = this.alertCtrl.create({
      title: 'Verify',
      message: 'Type code that was received via SMS',
      inputs: [
        {
          name: 'code',
          placeholder: 'Code'
        },
      ],
      buttons: [
        {
          text: 'Cancel',
          handler: data => {
            return;
          }
        },
        {
          text: 'Verify',
          handler: data => {
            this.verifyCode(data.code, verificationId);
          }
        }
      ]
    });
    promptCode.present();
  }

  private doLogin(): void {
    alert("VocĂȘ se logou com sucesso!");
    this.navCtrl.setRoot('HomePage');
  }
}

and html:

<ion-header>

  <ion-navbar>
    <ion-title>Login</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>

  <div class="login-verify" id="recaptcha-container"></div>

  <ion-list>
    <ion-item>
      <ion-label>+55</ion-label>
      <ion-input #phoneNumber type="tel" maxlength="9"></ion-input>
    </ion-item>
  </ion-list>

</ion-content>

<ion-footer class="login-footer">
  <button full ion-button class="login-btn" (click)="registerPhone()">Go</button>
</ion-footer>



from Ionic firebase phone auth plugin not installed

No comments:

Post a Comment