【问题标题】:How to implement Firebase phone authentication with Ionic 4?如何使用 Ionic 4 实现 Firebase 电话身份验证?
【发布时间】:2019-10-08 07:47:49
【问题描述】:

是否可以在移动应用中通过 Firebase 和 Ionic 4 使用电话身份验证?

我看过一些使用 Ionic 3 实现电话授权的旧教程,但这些似乎已经过时了。

firebaseui-web 项目不支持cordova apps 的电话身份验证,但我不确定这是否意味着 Firebase 电话身份验证对于 ionic 应用是不可能的。

如果您无法将 Firebase 的电话身份验证与 Ionic 4 一起使用,是否有替代的电话身份验证服务可以与 Ionic 4 一起使用?

【问题讨论】:

    标签: firebase ionic-framework firebase-authentication ionic4


    【解决方案1】:

    是的。您可以使用 Firebase 的 Javascript SDK 来完成,它需要用户通过验证码,然后向电话号码发送验证码,您可以使用该验证码登录和验证,这里解释了该过程:

    https://firebase.google.com/docs/auth/web/phone-auth#send-a-verification-code-to-the-users-phone

    【讨论】:

    • 我相信这在使用 ionic 构建移动应用时不起作用,因为 CAPTCHA 会中断。
    • 你找到解决办法了吗?
    • 我还没有找到预制的解决方案。我找到的最佳解决方案是创建服务器端点或云功能来自己进行电话验证(使用 Twilio 发送确认文本),然后使用服务器端 Firebase 管理 api 登录用户。
    【解决方案2】:

    问题在于,firebase auth 短信服务只会在应用处于生产模式(上传到商店)时发送消息。但是为了能够在测试模式下测试方法,它在firebase的白名单中添加了一个测试号。

    就我而言,我尝试了这些:

    短信验证.page.ts

    sendSmsVerification(phoneNumber): Promise <firebase.auth.UserCredential> {
        return new Promise((resolve, reject) => {
    
            firebase.auth().useDeviceLanguage();
            var verificationId;
            var code;
            const timeOutDuration = 60;
            const tell = '+54' + phoneNumber;
            this.FireBase.verifyPhoneNumber(tell, timeOutDuration).then(async (credential) => {
                // alert(credential.instantVerification);
                if (credential.verificationId) {
                    console.log("Android credential: ", credential);
                    verificationId = credential.verificationId;
                } else {
                    console.log("iOS credential: ", credential);
                    verificationId = credential;
                }
                if (credential.instantVerification) {
                    code = credential.code;
                    this.verifySms(verificationId, code)
                    .then( resp => {
                        resolve(resp);
                    })
                    .catch( err => {
                        reject(err);
                    });
                } else {
                    let prompt = await this.alertCtrl.create({
                        backdropDismiss: false,
                        header: 'Ingrese el codigo de confirmación del SMS.',
                        inputs: [{ name: 'confirmationCode', placeholder: 'Código de confirmación' }],
                        buttons: [
                            { text: 'Cancelar',
                            handler: data => { 
                                console.log('Cancel clicked');
                                resolve(data);
                            }
                            },
                            { text: 'Verificar',
                            handler: data => {
                                code = data.confirmationCode; 
                                this.verifySms(verificationId,code)
                                .then( resp => {
                                    resolve(resp);
                                })
                                .catch( err => {
                                    reject(err);
                                });                            }
                            }
                        ]
                    });
                    prompt.present();
                }
            }).catch(error => {
                console.log('Error! Catch SMSVerificacion', error);
                reject(error);
            });
        })
    }
    
    
    verifySms(verificationId, code): Promise <any> {
        console.log('parametros de verifySms ', verificationId +' ', code);
        const signInCredential = firebase.auth.PhoneAuthProvider.credential(verificationId,code);
        return firebase.auth().signInAndRetrieveDataWithCredential(signInCredential);
    }
    

    【讨论】:

    • 这里的 this.Firebase 是什么?
    • 这是 FirebaseX 的依赖注入。从'@ionic-native/firebase-x/ngx'导入{ FirebaseX };构造函数(公共 FireBase:FirebaseX){}
    • 嗨@jeronimoscarpalabrador,如果credential.instantVerification 返回的凭据不包含code 字段但id。我正在寻找一种将本机和 js firebase 混合用于电话身份验证的方法,但目前我没有处理 InstantVerification 的解决方案。 Dave Alden, who maintains FirebaseX Cordova plugin think that should not work
    【解决方案3】:

    是的,可以通过 Cordova 插件使用 firebase 电话身份验证,

    cordova-plugin-firebase-authentication

    将此插件添加到您的 ionic 4 项目中

    cordova plugin add cordova-plugin-firebase-authentication --save
    

    有了这个,我们可以在不使用 reCaptcha 的情况下验证手机。 请注意,这仅适用于真正的 android 设备,不适用于模拟器或浏览器。 功能实现

    verifyPhoneNumber(phoneNumber, timeout)
    
    cordova.plugins.firebase.auth.verifyPhoneNumber("+123456789", 30000)
    .then(function(verificationId) {
            // pass verificationId to signInWithVerificationId
    });
    

    AngularFire(带验证码)

    https://github.com/angular/angularfire

    首先,将 angularfire 库安装到您的项目中

    npm install firebase @angular/fire --save
    

    然后将这个库导入你的类

    import * as firebase from 'firebase/app';
    

    代码示例:

    firebase.auth().signInWithPhoneNumber(phoneNumber,recaptchaVerifier)
            .then(confirmationResult => {
              this.windowRef.confirmationResult = confirmationResult;
        })
    

    【讨论】:

    • 请尝试在您的答案中写下链接的内容,虽然仅链接的答案可能有效,但不推荐,因为如果链接脱机,它将丢失
    • 不适合我,我很害怕 verifyPhoneNumeber(...).then() is not a function
    猜你喜欢
    • 1970-01-01
    • 2018-06-17
    • 2021-06-02
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多