firebase登录验证

介绍 (Introduction)

Ever since Firebase was introduced, I thought it would have a significant effect on mobile development. The makers of Firebase have made that evident with authentication, real-time databases, machine learning, analytics, and so on.

自从引入Firebase以来,我一直认为它将对移动开发产生重大影响。 Firebase的制造商已通过身份验证,实时数据库,机器学习,分析等功能证明了这一点。

In this article, we’re going to learn how to use the FirebaseAuth library to authenticate users using phone number authentication via OTP with the resend feature.

在本文中,我们将学习如何使用FirebaseAuth库通过具有重新发送功能的OTP通过电话号码身份验证来对用户进行身份验证。

Let’s get started.

让我们开始吧。

为什么要电话号码认证? (Why Phone Number Authentication?)

There are a few benefits to using phone number-based authentication:

使用基于电话号码的身份验证有一些好处:

  • Avoid duplicate accounts: When you create an account based on a unique phone number, it’ll be hard to create multiple accounts.

    避免重复帐户: 当您基于唯一的电话号码创建帐户时,很难创建多个帐户。

  • Security: With phone number verification, users won’t have any passwords, so every time they log in, it can be done with a dynamic verification code sent directly to their mobile.安全性:通过电话号码验证,用户将没有任何密码,因此,每次登录时,都可以使用直接发送到手机的动态验证码来完成。
  • User experience: Phone number verification reduces the friction for the users to log in, as they don’t have to remember any password.用户体验:电话号码验证减少了用户登录的麻烦,因为他们不必记住任何密码。

为什么选择Firebase? (Why Firebase?)

Implementing phone number authentication involves sending an SMS to a user’s mobile. To do so, we need to pay for SMS services. But via Firebase, we can send an SMS without any cost. Firebase offers 10,000 verifications per month under the free plan.

实施电话号码身份验证涉及将SMS发送到用户的手机。 为此,我们需要支付SMS服务费用。 但是通过Firebase,我们可以免费发送短信。 根据免费计划,Firebase每月提供10,000个验证。

Apart from that, Firebase has been helpful to developers in several use cases, such as implementing a real-time database, authenticating users via social media platforms and phone numbers, monitoring analytics, sending notifications, and more.

除此之外,Firebase在多个用例中对开发人员有所帮助,例如实现实时数据库,通过社交媒体平台和电话号码对用户进行身份验证,监视分析,发送通知等。

A front-end developer can do all of this without any knowledge of server-side code. This is one of the core advantages of using Firebase. We can also implement Firebase on Android, IOS, and web seamlessly.

前端开发人员可以完成所有这些操作,而无需任何服务器端代码。 这是使用Firebase的核心优势之一。 我们还可以在Android,IOS和网络上无缝实施Firebase。

积分 (Integration)

Android development is rapidly adopting Kotlin, so in this article, we are going to implement the Firebase authentication through Kotlin. To add the FirebaseAuth library with ktx support, add the following line under the Dependencies tab at the module level of the build.gradle file:

Android开发正在Swift采用Kotlin,因此在本文中,我们将通过Kotlin实现Firebase身份验证。 要添加具有ktx支持的FirebaseAuth库,请在build.gradle文件的模块级别的“依赖关系”选项卡下添加以下行:

implementation 'com.google.firebase:firebase-auth-ktx:19.3.1'

Then we need to enable the phone number sign-in method in the Firebase console under the Authentication tab. Once you’re done with that, we can start coding.

然后,我们需要在Firebase控制台的“身份验证”标签下启用电话号码登录方法。 完成后,我们就可以开始编码了。

发送验证码 (Send Verification Code)

First, we need to create a callback listener to get notified about whether the user is successfully verified or not. For this, we need to use PhoneAuthProvider.OnVerificationStateChangedCallbacks(), as shown below:

首先,我们需要创建一个回调侦听器,以获取有关用户是否已成功验证的通知。 为此,我们需要使用PhoneAuthProvider.OnVerificationStateChangedCallbacks() ,如下所示:

val callbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {override fun onVerificationCompleted(credential: PhoneAuthCredential) {signInWithPhoneAuthCredential(credential)}override fun onVerificationFailed(e: FirebaseException) {}override fun onCodeSent(verificationId: String,token: PhoneAuthProvider.ForceResendingToken) {storedVerificationId = verificationIdresendToken = token}
}
  • onVerificationCompleted: This callback is invoked in two situations:

    onVerificationCompleted : 在两种情况下调用此回调:

  1. In a few cases, the mobile is automatically verified without the need for a verification code.在某些情况下,无需验证码即可自动验证手机。
  2. On some devices, Google Play services detect the incoming SMS and invoke the verification process without any action from the user.在某些设备上,Google Play服务会检测传入的SMS并调用验证过程,而无需用户采取任何措施。
  • onVerificationFailed: This callback is invoked for various reasons: if the mobile number format isn’t correct, when the app exceeds the SMS quota, or when the user entered the wrong verification code.

    onVerificationFailed :出于多种原因调用此回调:如果手机号码格式不正确,应用超出SMS配额或用户输入了错误的验证码。

  • onCodeSent: This callback is invoked when the code has been sent to the user’s mobile number.

    onCodeSent :当代码已发送到用户的手机号码时,将调用此回调。

  1. verificationId: We will get this as a parameter in the callback, which we should use to build credentials by combining it with the verification code.

    verificationId :我们将在回调函数中将此参数作为参数,通过将其与验证代码结合使用,我们可以使用该参数来构建凭证。

  2. resendtoken: Along with that, we will also receive resendtoken, which is used to resend the code later.

    resendtoken :除此之外,我们还将收到resendtoken ,用于稍后重新发送代码。

Now that we have created a callback listener, we need to collect the user’s mobile number and send the verification code. We can send the verification code by invoking the verifyPhoneNumber function on the PhoneAuthProvider instance with the necessary parameters. Have a look:

现在我们已经创建了一个回调侦听器,我们需要收集用户的手机号码并发送验证码。 我们可以通过使用必需的参数在PhoneAuthProvider实例上调用verifyPhoneNumber函数来发送验证码。 看一看:

PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber, // Phone number to verify60, // Timeout durationTimeUnit.SECONDS, // Unit of timeoutthis, // Activity instancecallbacks) // callback that we created earlier

Firebase won’t send multiple verification SMS until the prior request times out. So we need to maintain a flag in the activity onSaveInstanceState and retrieve it from onRestoreInstanceState to avoid any confusion.

在先前的请求超时之前,Firebase不会发送多条验证SMS。 因此,我们需要在活动onSaveInstanceState维护一个标志,并从onRestoreInstanceState检索它,以避免造成混淆。

When we create universal apps, it’s vital to support different languages. To send the verification message in a particular language, we can use setLanguageCode on the auth instance, as shown below:

当我们创建通用应用程序时,支持不同的语言至关重要。 要以特定语言发送验证消息,我们可以在auth实例上使用setLanguageCode ,如下所示:

auth.setLanguageCode("fr")

创建一个PhoneAuthCredential (Create a PhoneAuthCredential)

When the user enters the verification code, we need to create a PhoneAuthCredential using the verification code and the verificationid we received in onCodeSent callback earlier.

当用户输入的验证码,我们需要创建一个PhoneAuthCredential使用验证码和verificationid我们收到onCodeSent回调更早。

To create the PhoneAuthCredential object, call PhoneAuthProvider.getCredential:

要创建PhoneAuthCredential对象,请调用PhoneAuthProvider.getCredential

val credential = PhoneAuthProvider.getCredential(verificationId, OTP)

通过OTP验证 (Verify Through OTP)

Now it’s time to verify the mobile number. For this, we need to use signInWithCredential. We can add a listener to observe the state changes, as shown below:

现在该验证手机号码了。 为此,我们需要使用signInWithCredential 。 我们可以添加一个侦听器来观察状态变化,如下所示:

auth.signInWithCredential(credential).addOnCompleteListener(this) { task ->if (task.isSuccessful) {val user = task.result?.user// ...} else {// Sign in failed, display a message and update the UI}}

重新发送OTP (Resend OTP)

If everything goes well, the user might be logged in by this time. But what if they didn’t receive the OTP? To tackle this problem, developers usually enable the resend code button after the timeout.

如果一切顺利,则此时用户可能已登录。 但是,如果他们没有收到OTP,该怎么办? 为了解决此问题,开发人员通常在超时后启用重新发送代码按钮。

Now, the question is how to resend code with FirebaseAuth. It’s the same request (verifyPhoneNumber), but in this case, we need to add the resendToken that we got in the onCodeSent callback. Have a look:

现在,问题是如何使用FirebaseAuth重新发送代码。 这是同样的请求( verifyPhoneNumber ),但在这种情况下,我们需要添加resendToken我们在得到onCodeSent回调。 看一看:

PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber, // Phone number to verify60, // Timeout durationTimeUnit.SECONDS, // Unit of timeoutthis, // Activity instancecallbacks, // callback that we created earlierresendToken) // resend token

结论 (Conclusion)

To learn more about Firebase tips and new features, read the following articles:

要了解有关Firebase提示和新功能的更多信息,请阅读以下文章:

  • “Firebase — A perfect database for your app”

    “ Firebase-您的应用程序的理想数据库 ”

  • “Machine Learning in Android with Firebase”

    “使用Firebase在Android中进行机器学习 ”

That is all for now. Hope you learned something useful. Thanks for reading!

到此为止。 希望你学到了一些有用的东西。 谢谢阅读!

翻译自: https://medium.com/better-programming/phone-number-authentication-with-firebase-5a28ec959dca

firebase登录验证


http://www.taodudu.cc/news/show-4569997.html

相关文章:

  • 微信小程序中的正则验证、手机号验证随笔
  • [Android]手机短信验证功能
  • 互联网金融:为什么贷前风控需要进行手机号码验证(联通、移动、电信)
  • 手机工行显示服务器,工行手机银行服务器安全证书验证失败
  • 解决raw.githubusercontent.com地址DNS污染的方法参考
  • 「经济读物」小岛经济学:鱼、美元和经济的故事
  • ThreadLocal之强、弱、软、虚引用
  • 7号信令1-1
  • 7号信令1-3
  • 【5G】协议与信令区别
  • 来自中国的人工智能解决方案,如何风靡全球数亿用户?
  • python md5加密和统一社会信用代码_统一社会信用代码校验位python实现
  • 该爬破解验证码,爬企信宝必须破解滑块验证
  • 商务部研究院信用所、启信宝联合发布《中国商务信用发展指数报告(2022)》
  • 启xin宝app的token算法破解——逆向篇(二)
  • 启信宝受益所有人界面_希望您的项目成功吗? 然后确保其他人也可以从您的想法中受益。...
  • 启信宝牵手国家队,征信服务进化再加速
  • 产品3周迭代一次,启信宝驾驭8000万企业征信的平台架构
  • 启信宝发布植树节产业洞察:超2000家绿色造纸企业,造纸业迭代落后产能
  • 2018信用服务业市场:天眼查、企查查、启信宝、企查猫处第一梯队
  • python爬取启信宝_requests,lxml爬启信宝
  • ABAP调用启信宝HTTP restful API实例
  • IDea中maven项目实现对接企查查、启信宝案例
  • 有鱼吃,何必再抓老鼠?-- 2010高考作文,猫捉老鼠的故事...
  • PC安装MAC系统
  • ARM体系结构(重制版)——九鼎创展 x210V3s
  • 前导图法(PDM)或单代号网络(AON)总结
  • 【庖丁解牛系列】 项目时间管理之前导图/单代号网络图
  • 单代号网络图计算例题_常用连续随机变量的关系与密度函数的计算
  • excel 画散点图 怎么设置图片的分辨率_双代号时标网络图用Excel画

firebase登录验证_使用Firebase进行电话号码身份验证相关推荐

  1. 什么学习软件需要身份证验证_什么是两层身份验证,为什么我需要它?

    什么学习软件需要身份证验证 More and more banks, credit card companies, and even social media networks and gaming ...

  2. win7 远程计算机需要网络级别身份验证,而您的计算机不支持该验证,Windows远程登录:远程计算机需要网络级别身份验证,而您的计算机不支持该验证,请联系...

    问题是这样的:在xp下远程连接windows7/windows2008,由于远程win7/win2008计算机在开启远程桌面的时候选中了"仅允许运行使用网络级别身份验证的远程桌面计算机连接& ...

  3. [转][.NET 基于角色安全性验证] 之三:ASP.NET Forms 身份验证

    在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数. 2. 创建登录页. 登录页中的操作包括: 1. 验证用户名和密码是否正确. 2. 创建身份 ...

  4. 两步验证杀手锏:Java 接入 Google 身份验证器实战

    转载自   两步验证杀手锏:Java 接入 Google 身份验证器实战 什么是两步验证? 大家应该对两步验证都熟悉吧?如苹果有自带的两步验证策略,防止用户账号密码被盗而锁定手机进行敲诈,这种例子屡见 ...

  5. 使用Xamarin.Android中的Google登录OAuth 2.0对用户进行身份验证

    什么是用户认证? (What is User Authentication?) There is a user base associated with every app that is avail ...

  6. SQL Server 登录更换【Windows身份验证】为【SQL Server 身份验证】

    1.安装好 SQL Server 后,使用 Windows 身份验证登陆SQL,然后在服务器上右键,选择[属性],在弹出的窗口中选择[安全性],在服务器身份验证项里勾选[SQL Server 和 Wi ...

  7. java授权失败_自定义Spring Security的身份验证失败处理方法

    1.概述 在本快速教程中,我们将演示如何在Spring Boot应用程序中自定义Spring Security的身份验证失败处理.目标是使用表单登录方法对用户进行身份验证. 2.认证和授权(Authe ...

  8. linux用户登录身份验证错误,启动 WLS 时的身份验证错误(解决linux下问题)

    The WebLogic Server did not start up properly. Reason: weblogic.security.SecurityInitializationExcep ...

  9. 创建steam账户反复人机验证_您必须先通过人机验证才能创建steam帐户怎么办

    展开全部 在注册steam帐户遇到提示必须通过人机验证才能创建62616964757a686964616fe4b893e5b19e31333433643062提示时,勾选注册页面中的进行人机验证.在人 ...

最新文章

  1. python入门必备10个坑_适合 Python 初学者的一些技巧和坑
  2. angle-class
  3. ECMAScript 5 新增 Object 接口
  4. 关于vue执行打包后,如何在本地浏览问题
  5. 智慧交通day03-车道线检测实现07:车道曲率和中心点偏离距离计算+代码实现
  6. 计算机巧用剪纸做画册教案,3 巧折巧剪教案公开课一等奖
  7. jquery选择器之属性选择器
  8. Windows系统安装Redis(详细)
  9. Oracle下载账户
  10. AXure交互设计指南
  11. 项目开发中DEV、QAS、PRD是什么意思
  12. 微带滤波器摘要_微带滤波器设计
  13. 8021什么意思_无线网络标准IEEE802.11n是什么意思
  14. android 汉字字母排序,android recycleView自定义字母检索A-Z排序滑动通讯录汉字英文相互转换...
  15. 音频合并的步骤有哪些
  16. 前端基础教程:简单的实现html+css+javascript点赞效果
  17. 第1关:knn算法概述
  18. 数字图像处理:python对图像做傅里叶变换,理想低通滤波器,理想高通滤波器
  19. 怎样找自己研究领域的论文
  20. Redis 编译报zmalloc.h相关的错

热门文章

  1. 创业者手记:我所犯的那些入门错误
  2. 网页制作基础教程(网址)
  3. 北疆蓝盾 新城区消防大队开展住宅小区消防通道、电动车专项治理行动
  4. python发票二维码条码识别_Python zxing 库解析(条形码二维码识别)
  5. 高数--反常积分与无穷级数的关系
  6. 算法模板ACW(更新中)
  7. 弘辽科技:义乌商家操盘手:怎么把3.8元奥运同款卖成全网第一
  8. 诺基亚C5智能手机的功能非常好,并具有一定程度的可取性
  9. 跨境独立建站_做外贸为什么独立建站更重要?
  10. 【Web系列二十】Django+Celery+Asgiref+Channels+协程锁实现Websocket异步并发