栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Firebase在Flutter中进行电话身份验证?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Firebase在Flutter中进行电话身份验证?

记录良好的工作演示项目在这里

下面是详细步骤

脚步

  1. 询问用户的电话号码
  2. 从Firebase获取OTP
  3. 登录到Firebase

规则

  • 登录/登录以相同的方式完成。
  • OTP仅用于获取
    AuthCrendential
    对象
  • AuthCredential
    object是用于登录用户的唯一内容。据或者从所获得
    verificationCompleted
    的回调函数在
    verifyPhoneNumber
    或从
    PhoneAuthProvider

(不要担心它是否令人困惑,请继续阅读,您会明白的)

工作流程

  1. 用户给
    phoneNumber
  2. Firebase发送OTP
  3. 登录用户
    • 如果带有的SIM卡
      phoneNumber
      不在当前运行该应用的设备中,
    • 我们必须先询问OTP并获取
      AuthCredential
      对象
    • 接下来,我们可以使用它
      AuthCredential
      来登录即使
      phoneNumber
      设备中存在
    • 否则,如果用户提供的SIM电话号码在运行该应用的设备中,
    • 我们可以不使用OTP登录。
    • 因为
      verificationCompleted
      from
      submitPhoneNumber
      函数的回调提供了
      AuthCredential
      登录用户所需的对象
    • 但在以前的情况下,由于SIM卡不在手机中,因此未调用。

功能

  • 提交电话号码
    Future<void> _submitPhoneNumber() async {        /// NOTE: Either append your phone number country pre or add in the pre itself        /// Since I'm in India we use "+91 " as prefix `phoneNumber`        String phoneNumber = "+91 " + _phoneNumberController.text.toString().trim();        print(phoneNumber);        /// The below functions are the callbacks, separated so as to make pre more readable        void verificationCompleted(AuthCredential phoneAuthCredential) {          print('verificationCompleted');          ...          this._phoneAuthCredential = phoneAuthCredential;          print(phoneAuthCredential);        }        void verificationFailed(AuthException error) {          ...          print(error);        }        void preSent(String verificationId, [int pre]) {          ...          print('preSent');        }        void preAutoRetrievalTimeout(String verificationId) {          ...          print('preAutoRetrievalTimeout');        }        await FirebaseAuth.instance.verifyPhoneNumber(          /// Make sure to prefix with your country pre          phoneNumber: phoneNumber,          /// `seconds` didn't work. The underlying implementation pre only reads in `milliseconds`          timeout: Duration(milliseconds: 10000),          /// If the SIM (with phoneNumber) is in the current device this function is called.          /// This function gives `AuthCredential`. Moreover `login` function can be called from this callback          verificationCompleted: verificationCompleted,          /// Called when the verification is failed          verificationFailed: verificationFailed,          /// This is called after the OTP is sent. Gives a `verificationId` and `pre`          preSent: preSent,          /// After automatic pre retrival `tmeout` this function is called          preAutoRetrievalTimeout: preAutoRetrievalTimeout,        ); // All the callbacks are above      }
  • 提交OTP
    void _submitOTP() {        /// get the `smsCode` from the user        String smsCode = _otpController.text.toString().trim();        /// when used different phoneNumber other than the current (running) device        /// we need to use OTP to get `phoneAuthCredential` which is inturn used to signIn/login        this._phoneAuthCredential = PhoneAuthProvider.getCredential( verificationId: this._verificationId, smsCode: smsCode);        _login();      }
  • 登录/登录
    Future<void> _login() async {        /// This method is used to login the user        /// `AuthCredential`(`_phoneAuthCredential`) is needed for the signIn method        /// After the signIn method from `AuthResult` we can get `FirebaserUser`(`_firebaseUser`)        try {          await FirebaseAuth.instance   .signInWithCredential(this._phoneAuthCredential)   .then((AuthResult authRes) { _firebaseUser = authRes.user; print(_firebaseUser.toString());          });          ...        } catch (e) {          ...          print(e.toString());        }      }
  • 登出
      Future<void> _logout() async {        /// Method to Logout the `FirebaseUser` (`_firebaseUser`)        try {          // signout pre          await FirebaseAuth.instance.signOut();          _firebaseUser = null;          ...        } catch (e) {          ...          print(e.toString());        }      }

有关实现的更多详细信息,请参考此处的

lib/main.dart
文件。

如果发现问题,欢迎对此答案和此自述文件进行编辑



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号