Flutter Firebase Auth匿名currentUser()返回null [英] Flutter Firebase Auth Anonymous currentUser() returns null
问题描述
我们已经开发并测试了Flutter应用.一切都按预期进行.我们部署了一个应用程序来存储并赢得一些用户.
We have developed and tested a Flutter app. Everything works as expected. We deployed an app to stores and gain some users.
几个月后,我们收到用户的投诉,称他们在打开应用程序时丢失了所有数据.
After a few months, we got complaints from our users that they lost all their data when they opened the app.
我相信数据不会丢失,只有他们的匿名帐户更改为新的匿名帐户.
I believe that the data is not lost, only their anonymous account changed to the new anonymous account.
丢失数据的用户有哪些共同点:
What users, with lost data, have in common:
- iOS设备(不确定是否相关)
- 至少有几天没有使用该应用程序.
我们的用户身份验证流程:
Our user authentication flow:
- 应用启动,
- 所有服务均通过
main()
方法初始化 -
AuthService
初始化final _auth = FirebaseAuth.instance;
并调用loginUser()
, -
loginUser()
方法执行:
- App starts,
- All services are initialized in
main()
method, AuthService
initializefinal _auth = FirebaseAuth.instance;
and callsloginUser()
,loginUser()
method executes:
void loginUser() async {
FirebaseUser user = await _auth.currentUser();
if (user == null) {
AuthResult result = await _auth.signInAnonymously();
user = result.user;
}
IdTokenResult userToken = await user.getIdToken();
print('USER');
print(' UID: ${user.uid}');
print(' Token: ${userToken.token}');
print(' Expires: ${userToken.expirationTime}');
}
我们正在使用版本:
-
flutter: v1.12.13+hotfix.7
-
firebase_core: ^0.4.0+8
-
firebase_auth: ^0.14.0+5
flutter: v1.12.13+hotfix.7
firebase_core: ^0.4.0+8
firebase_auth: ^0.14.0+5
注册用户没有遇到任何问题.
We didn't experience any problems with registered users.
问题:
- 在什么情况下
this._auth.currentUser()
返回null? - 该匿名帐户是否有有效期限?
- 是否有办法重现此问题?
- 有人遇到过同样的问题吗?
- 我们在做什么错了?
- 如何为匿名用户避免这种情况?
- In what cases would
this._auth.currentUser()
return null? - Is there an expiry date for the anonymous account?
- Is there a way to reproduce this issue?
- Did someone experience the same issue?
- What are we doing wrong?
- How can this be avoided for anonymous users?
推荐答案
只需扩展@mFeinstein的答案
Just extending on @mFeinstein's answer
匿名身份验证令牌将保留在磁盘上,并且仅在再次调用signInAnonymously
时才会重新生成.如果不看具体的实现,这很难说,但是可能存在一些次要问题,导致在调用currentUser()
时返回null会导致生成新的匿名用户ID.
Anonymous auth token is persisted to disk and is only regenerated when calling signInAnonymously
again. Without looking at the specific implementation, it's hard to tell, but there could've been some secondary issue that lead to returning null when calling currentUser()
leading to generating a new anonymous user id.
如何为匿名用户避免这种情况?
- 不是使用匿名登录,而是使用自定义身份验证提供程序登录,并将生成的令牌存储在加密的钥匙串中,以便将其备份到iCloud.
有关自定义提供程序身份验证的更多信息,请参见以下网址: https://firebase.google.com/docs/auth/ios/custom-auth
More info on custom provider auth can be found here: https://firebase.google.com/docs/auth/ios/custom-auth
这篇关于Flutter Firebase Auth匿名currentUser()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!