离线时无法在flutter app中使用signInWithEmailAndPassword

问题描述 投票:-1回答:3

我正在尝试在同样使用Cloud Firestore的Flutter应用中使用Firebase Auth。我遇到的问题是离线时要做什么。

我记下用户名和密码,并尝试使用以下代码登录:

    _user = await _auth.signInWithEmailAndPassword(email: account, password: myPassword).catchError((error) {
  print(error);
});

await _user.getIdToken().catchError((error) {
  // The error source may be that they are not currently connected to the internet
  print(error);
});
final FirebaseUser currentUser = await _auth.currentUser();

问题是当我离线时signInWithEmailAndPassword失败。我需要FirebaseUser对象才能访问Cloud Firestore。我已经阅读了有关身份验证状态持久性的信息,但这似乎不适用于Flutter。

我知道这必须有效,而且我太新了,无法理解如何在离线设置中使用Firebase Auth和Cloud Firestore。我错过了什么?

firebase-authentication flutter google-cloud-firestore google-authentication
3个回答
1
投票

解决方案是测试用户是否已经过身份验证。 Firebase文档说:

如果您的应用使用Firebase身份验证,则Firebase实时数据库客户端会在应用重新启动后保留用户的身份验证令牌。如果身份验证令牌在您的应用程序脱机时到期,则客户端会暂停写入操作,直到您的应用程序重新对用户进行身份验证,否则写入操作可能会因安全规则而失败。

所以我只是检查一下。这是代码:

// If user is already logged in, currentUser will return the authenticated user
// Otherwise, _user will be null.
_user = await _auth.currentUser();
if(_user == null) {
  _user = await _auth.signInWithEmailAndPassword(email: account, password: myPassword).catchError((error) {
    print(error);
  });
}

0
投票

Firebase身份验证在离线时无法运行。绝对需要能够连接到Firebase后端服务,以便验证用户的帐户并更新用于在线时访问其他Firebase服务的用户ID令牌。否则,系统将不会真正安全。


0
投票

如何在不连接到身份验证服务器的情况下验证用户身份? 您需要一个活动连接来验证用户身份。 您可以将Firebase Cloud Firestore / Realtime数据库数据存储在手机中(磁盘持久性),但不能存储身份验证,因为这是为了防止从本地存储中滥用访问。

因此,处理此问题的一种方法是仅在用户的设备为connected to the network和/或connected to Firebase时显示登录对话框。

© www.soinside.com 2019 - 2024. All rights reserved.