Ionic Firebase电话auth插件未安装

时间:2018-10-03 17:20:42

标签: android angular typescript ionic-framework

我正在使用以下插件在ionic上进行电话验证:https://github.com/sajTempler/IonicFirebasePhoneAuth,即时通讯使用的国家代码为55,操作员编号为15。据我所调试,我的代码没有任何问题。每当我在真实设备上运行它时,控制台都会记录registerPhone err plugin_not_installed,但我不知道它指的是哪个插件。

这是代码

    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');
  }
}

和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>

1 个答案:

答案 0 :(得分:0)

您必须使用Cordova吗?您是如何安装Firebase插件的?可以通过您的应用程序访问吗?

您是否运行过此命令(如here所述):

nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(j<drawablearray.length){
                quoteTextView.setBackgroundResource(drawablearray[j]);
                quoteTextView.getBackground().setAlpha(150);
                j++;
            }else{
                quoteTextView.setBackgroundResource(R.drawable.girl);
                quoteTextView.getBackground().setAlpha(150);
            }
        }
    });