【问题标题】:Flutter & Firebase- How to allow only 1 account per deviceFlutter & Firebase - 如何只允许每台设备有 1 个帐户
【发布时间】:2021-08-22 23:34:35
【问题描述】:

我试图只允许用户在设备上创建 1 个帐户,因此当他创建帐户并注销,然后尝试创建新帐户时,它不会创建帐户并说此设备已经有一个帐户.到目前为止,我得到了设备 ID,但我不知道现在该做什么。

【问题讨论】:

  • 解决方案是获取设备的唯一 ID 并使用用户详细信息对其进行修补。因此,当任何人或该特定用户尝试从该特定设备创建帐户时,您将使用device_info_plus 包检查后端是否存在该唯一 ID,检查此处stackoverflow.com/questions/45031499/…
  • 我如何检查它是否存在

标签: firebase flutter dart google-cloud-firestore firebase-authentication


【解决方案1】:

我要做的是使用唯一的设备 id 并在 firestore 中在用户集合中创建一个文档,其文档 id 是设备 id,然后验证该 id 是否已经存在,它将使用以下代码

FirebaseFirestore.instance
    .collection('users')
    .doc(idDevice)
    .get()
    .then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        print('Document data: ${documentSnapshot.data()}');
      } else {
        print('Document does not exist on the database');
      }
    });

【讨论】:

    【解决方案2】:

    在您的数据库中创建一个表,其中组合列:userid(来自新帐户)和 deviceid 是唯一的。 使用 device_info 包获取唯一的设备 ID

    方法获取设备ID

    Future<String> getDeviceId() async {
      var deviceInfo = DeviceInfoPlugin();
      if (Platform.isIOS) {
        var iosInfo= await deviceInfo.iosInfo;
        return iosInfo.identifierForVendor; // on iOS
      } else {
        var androidInfo = await deviceInfo.androidInfo;
        return androidInfo.androidId; // on Android
      }
    }
    

    【讨论】:

    • DeviceInfoPlugin 来自哪里?
    猜你喜欢
    • 2019-08-13
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2021-06-14
    • 2017-01-15
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多