繁体   English   中英

如何使用 flutter 检查电话号码是否已在 firebase 身份验证中注册

[英]How to check if phone number is already registered in firebase authentication using flutter

因此,我在 flutter 应用程序中制作了一个简单的注册和登录屏幕,该应用程序使用 firebase 的电话身份验证。 为了注册,我可以注册新用户,因为用户提供了他的电话号码并获得了 OTP。 但是对于登录,我想检查输入的号码是否已经注册。 如果是这样,他会获得 otp 并登录,或者如果未注册,则要求先注册。

Firebase 管理员 SDK 支持这个。 以下是如何设置 firebase 管理员(文档)。 After you set up admin, you can use cloud_functions package to call APIs from the firebase admin SDK and the API we'll be using is one that allows us to get a user by phone number ( documentation ). 如果 API 响应是用户记录,则我们知道电话存在。

在此示例中,我使用的是 node.js。 在函数/index.js 中:

exports.checkIfPhoneExists = functions.https.onCall((data, context) => {
   const phone = data.phone
   return admin.auth().getUserByPhoneNumber(phone)
    .then(function(userRecord){
        return true;
    })
    .catch(function(error) {
        return false;
    });
});

在您的 dart 代码中:

final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(functionName: 'checkIfPhoneExists');
dynamic resp = await callable.call({'phone': _phone});
if (resp.data) {
    // user exists
}

将 OTP 发送给用户后,您可以在验证 OTP function 中验证用户是新用户还是现有用户

verifyOtp(String input, context) async {
  String retVal = "error";
  OurUser _user = OurUser();
  print(input);
  final AuthCredential credential = PhoneAuthProvider.credential(
      verificationId: _verificationId, smsCode: input);
  try {
    //  await _auth.signInWithCredential(credential);
    UserCredential _authResult = await _auth.signInWithCredential(credential);

    // Here i have to save the details of the user in the database
    if (_authResult.additionalUserInfo.isNewUser) {
      currentUser.uid = _authResult.user.uid;
      currentUser.phone = _inputText;
      currentUser.type = "Customer";

      retVal = await OurDatabase().createUser(currentUser);
    } else {
      // get the information of the user from the database this already exists
      currentUser = await OurDatabase().getUserInfo(_authResult.user.uid);
      if(currentUser!= null) {
        Navigator.pushNamedAndRemoveUntil(
            context, "/homescreen", (route) => false);
      }
    }
    print("End of the await");

    // when signup with the otp
    if (retVal == "success") {
      print("why not inside this mane");
      Navigator.pushNamedAndRemoveUntil(
          context, "/homescreen", (route) => false);
    }

    saveAllData();
  } catch (e) {
    print(e);
    print("Something went wrong");
    //prin
  }
}

现在,当您想从用户那里验证 OTP 并且在验证顶部之后,您可以知道该用户确实是新用户还是旧用户,但是如果您想事先知道,那么最好的解决方案是在 Firestore 中创建一个新集合,该集合只有一个文档(因此您只需为读取一个文档付费),其中仅包含在您的应用程序中注册的所有用户数,

我使用了一种简单直接的方式,效果很好。 首先,在用户创建帐户时,将手机号码添加到单独节点中的 firebase 数据库中。

 await dbref.child("RegisteredNumbers").push().set({
        "phoneNo": FirebaseAuth.instance.currentUser!.phoneNumber,
      });

每当用户尝试登录或注册时,请检查此节点中提供的号码是否可用。

 Future<bool> checkNumberIsRegistered({required String number}) async {
    bool isNumberRegistered = false;
    try {
      await dbref.child("RegisteredNumbers").once().then((data) {
        for (var i in data.snapshot.children) {
          String data = i.child("phoneNo").value.toString();

          if (number == data) {
            isNumberRegistered = true;
            return isNumberRegistered;
          } else {
            isNumberRegistered = false;
          }
        }
      });
      return isNumberRegistered;
    } catch (e) {
      return false;
    }
  }

希望能帮助到你

暂无
暂无

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

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