Firebase Unity 12.2当前用户邮箱更新后无法刷新问题求助
Firebase Unity 邮箱更新后ReloadAsync不生效的解决方案
核心问题原因
调用ReloadAsync后没拿到新邮箱,大概率是因为复用了之前创建的FirebaseUser实例。虽然ReloadAsync会同步服务器数据到本地实例,但在Unity的Firebase SDK中,有时需要重新从Auth.CurrentUser获取最新实例,才能拿到更新后的属性。
是否需要强制登出?
不需要。强制登出是下策,优先通过重新获取用户实例的方式解决,只有当这种方法无效时再考虑登出重进。
代码调整建议
1. 刷新邮箱显示的代码修改
将原来复用旧实例的逻辑,改为在ReloadAsync完成后重新获取当前用户实例:
void Start() { FirebaseManager firebaseManager = FirebaseManager.Instance; // 先触发ReloadAsync同步服务器数据 firebaseManager.Auth.CurrentUser?.ReloadAsync().ContinueWithOnMainThread(task => { if (task.IsCompletedSuccessfully) { // 重新获取最新的用户实例 Firebase.Auth.FirebaseUser updatedUser = firebaseManager.Auth.CurrentUser; if (updatedUser == null) { EventsManager.Instance.AuthLogout(); } else { currentEmailPlain = updatedUser.Email; CurrentEmail.text = Helpers.MaskEmail(currentEmailPlain); } } }); }
2. 额外优化方案
如果仅在Start时刷新仍有问题,可以监听Auth的用户状态变化事件,当用户信息更新时自动刷新UI:
void OnEnable() { FirebaseManager.Instance.Auth.StateChanged += OnAuthStateChanged; } void OnDisable() { FirebaseManager.Instance.Auth.StateChanged -= OnAuthStateChanged; } private void OnAuthStateChanged(object sender, Firebase.Auth.AuthStateChangedEventArgs e) { if (e.User != null) { currentEmailPlain = e.User.Email; CurrentEmail.text = Helpers.MaskEmail(currentEmailPlain); } }
内容的提问来源于stack exchange,提问作者Ando
相关产品推荐
相关产品推荐

