简体   繁体   English

离子电容 Firebase 电话号码认证问题

[英]Ionic Capacitor Firebase Phone Number Authentication Problems

For anyone who is trying to authenticate via firebase phone number using ionic, this post is for you.对于尝试使用 ionic 通过 firebase 电话号码进行身份验证的任何人,这篇文章适合您。

I spent weeks trying to solve this problem for my own app, and have moved on to using email because solving the phone number problem was taking too much time, but I am making this post to save you from wasting your time on red herrings.我花了数周时间尝试为我自己的应用程序解决这个问题,并开始使用 email,因为解决电话号码问题需要花费太多时间,但我发布这篇文章是为了避免您将时间浪费在红鲱鱼上。

The Issue:问题:

Phone number can only be used when attached to a url, so if you are making an ionic application through a website, phone number auth via firebase will work, however, if you are trying to do it on mobile it won't work because firebase requires a url attached and there are no urls in mobile apps of course ( firebase ).电话号码只能在附加到 url 时使用,因此如果您通过网站进行离子应用程序,则通过 firebase 的电话号码身份验证将起作用,但是,如果您尝试在移动设备上进行操作,它将无法正常工作,因为 ZC3ABF12E152FCC215需要附加 url 并且移动应用程序中当然没有网址( firebase )。

Option 1: Plugins选项 1:插件

So, the easiest option would be to look around for third-party plugins to implement phone number auth on.因此,最简单的选择是寻找第三方插件来实现电话号码身份验证。 I believe there is a Cordova plugin for this, ( cordova-plugin-firebase-authentication ), however that library is not compatible with Capacitor (compatable plugins ).我相信有一个 Cordova 插件( cordova-plugin-firebase-authentication ),但是该库与电容器不兼容(兼容插件)。

There are a few other plugins that I tried to implement without much success.我尝试实现其他一些插件但没有取得多大成功。 Notably, the ( capacitor-firebase-auth plugin ).值得注意的是(电容器-firebase-auth 插件)。 It claims that phone auth works for ios and android, however, the actual method for doing that seems very complicated and follows a different path using.p8 codes that I was unable to follow-through with ( confusion ).它声称电话身份验证适用于 ios 和 android,但是,执行此操作的实际方法似乎非常复杂,并且使用我无法跟进的.p8 代码遵循不同的路径( 混淆)。 And this plugin has some major limitations .而且这个插件有一些主要的限制

Option 2: In-App-Browsers选项 2:应用内浏览器

Since option one doesn't seem very feasible right now, the second option is to put code up on a website for phone number authentication and then open a browser within your app to authenticate.由于选项一目前似乎不太可行,因此第二个选项是将代码放在网站上进行电话号码验证,然后在您的应用程序中打开浏览器进行验证。 I spent a ton of time trying to get this method to work to no avail.我花了很多时间试图让这种方法无效。 It seems possible theoretically, but in practice, it is a mess.理论上似乎可行,但在实践中,它是一团糟。 The biggest problem with this method is that what you want is to be able to run the signInWithPhoneNumber(phoneNumber, appVerifier) on your mobile app, because then your app treats users that have signed in through phone number like any other user and it makes general authentication much easier downstream in your application.这种方法的最大问题是您想要的是能够在您的移动应用程序上运行signInWithPhoneNumber(phoneNumber, appVerifier) ,因为这样您的应用程序就会像对待任何其他用户一样对待通过电话号码登录的用户,这使得通用在您的应用程序的下游更容易进行身份验证。 However, in order to do that you need a verified appVerifier , which is of the class firebase.auth.RecaptchaVerifier .但是,为了做到这一点,您需要经过验证的appVerifier ,它属于 class firebase.auth.RecaptchaVerifier The firebase.auth.RecaptchaVerifier is supposed run .render() to initiate the recaptcha provlem and then run .verify() to verify a token that is given after the user does the recaptcha ( documentation ). firebase.auth.RecaptchaVerifier应该运行 .render .render()来启动 recaptcha provlem,然后运行.verify()来验证用户执行 recaptcha 后给出的令牌( 文档)。 I have tried to run .render() on a website and then .verify() on a mobile app, passing the token between the two.我尝试在网站上运行.verify() .render()然后在移动应用程序上运行 .verify(),在两者之间传递令牌。 I am not sure why this doesn't work, it could just be a problem with my setup, but after trying for over a week on this method, I gave up on trying to get it to work.我不确定为什么这不起作用,这可能只是我的设置有问题,但是在尝试了这个方法一个多星期后,我放弃了尝试让它工作。 The token I pass just doesn't confirm.我通过的令牌只是没有确认。 I don't know how these two functions work under the hood, there may be some identification system for each unique instance of firebase.auth.RecaptchaVerifier that is invalidating this method, I really don't know.我不知道这两个函数是如何工作的,对于firebase.auth.RecaptchaVerifier的每个唯一实例可能有一些识别系统使这种方法无效,我真的不知道。

If you want to take a stab at this method, I recommend using the cordova plugin (it is compatible on capacitor projects) instead of an iframe or the capacitor browser plugin for in-app-browser to the website because it is easier to use and there is better functionality for passing data between the app and the website.如果您想尝试这种方法,我建议使用cordova 插件(它与电容器项目兼容)而不是 iframe 或用于网站内应用程序浏览器的电容器浏览器插件,因为它更易于使用和在应用程序和网站之间传递数据有更好的功能。

Option 3: Implementing Manually on IOS and Android选项 3:在 IOS 和 Android 上手动实施

I haven't tried out this method, however, theoretically, you could go into your android studio and Xcode projects to actually write swift and java code following the firebase instructions for firebase phone auth ( android tutorial ) ( ios tutorial ). I haven't tried out this method, however, theoretically, you could go into your android studio and Xcode projects to actually write swift and java code following the firebase instructions for firebase phone auth ( android tutorial ) ( ios tutorial ). This would probably work, but I am not sure how you would notify your ionic project to defer to the swift and java files when authenticating.这可能会起作用,但我不确定您将如何通知您的 ionic 项目在进行身份验证时遵循 swift 和 java 文件。


Please let me know if there are any methods that have worked for you or any solutions to the roadblocks I have encountered, this is what I've tried so far!请让我知道是否有任何对您有用的方法或任何解决我遇到的障碍的方法,这是我迄今为止尝试过的!

I am the OP.我是OP。 After doing more work I finally got the phone auth system to work using capacitor-firebase-auth ( https://github.com/baumblatt/capacitor-firebase-auth ).在做了更多的工作之后,我终于让电话身份验证系统使用电容器火力基础身份验证( https://github.com/baumblatt/capacitor-firebase-auth )工作。 I also created a git repo showing my code example and how to use it: https://github.com/Darrow8/capacitor-phone-test我还创建了一个 git 存储库,展示了我的代码示例以及如何使用它: https://github.com/Darrow8/capacitor-phone-test

In my previous attempt, I was running into issues with getting cfaSignInPhone to work but after more tweaking, it worked.在我之前的尝试中,我遇到了让cfaSignInPhone工作的问题,但经过更多调整后,它工作了。 Another major issue was getting firebase.auth().signInWithCredential to work.另一个主要问题是让firebase.auth().signInWithCredential工作。 It turns out I had to combine the aformentioned capacitor-firebase-auth library with @angular/fire https://www.npmjs.com/package/@angular/fire事实证明,我必须将上述的capacitor-firebase-auth库与@angular/fire https://www.npmjs.com/package/@angular/fire结合起来

You can simply use cordova-plugin-firebase-authentication its compatible with capacitor, it works for me.您可以简单地使用与电容器兼容的cordova-plugin-firebase-authentication,它对我有用。

https://ionicframework.com/docs/native/firebase-authentication https://ionicframework.com/docs/native/firebase-authentication

Hello I tried working with Firebase Web and it worked well for me.您好,我尝试与 Firebase Web 合作,它对我来说效果很好。 No external plugins just firebase web implementation没有外部插件,只有 firebase web 实现

Cannot leave a comment due to reputation but I would appreciate an implementation of it, I am going through your code but not sure where to call the methods you have mentioned.由于声誉而无法发表评论,但我希望能实现它,我正在浏览您的代码,但不确定在哪里调用您提到的方法。 Any feedback would be most welcome.任何反馈都将受到欢迎。 Bests最好的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM