如何观察firebase.auth().currentUser.displayName和其他属性的更改? [英] How to observe firebase.auth().currentUser.displayName and other attributes for changes?

查看:91
本文介绍了如何观察firebase.auth().currentUser.displayName和其他属性的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase身份验证,并且当用户更改其个人资料(例如其displayNamephotoURLemail等)时,不会触发onAuthStateChanged回调,但是底层的firebase.auth().currentUser自动更新.

I am using firebase auth and when users change their profile, such as their displayName, photoURL, or email etc. the onAuthStateChanged callback won't be fired, but the underlying firebase.auth().currentUser gets updated automatically.

我需要在用户更新其个人资料后更新UI,即我想保持firebase.auth().currentUser与客户端存储(尤其是vuexredux类似的存储)同步.

I need to update the UI after the user updates their profile, ie. I would like to keep firebase.auth().currentUser in sync with the client store (specifically vuex or redux-like store).

是否可以观察firebase.auth().currentUser的更改,或者可以使用其他任何钩子在firebase.auth().currentUser更改时得到通知?

Is there a way to observe firebase.auth().currentUser for changes, or any other hooks I can use to get notified when firebase.auth().currentUser changes?

谢谢!

推荐答案

我遇到了同样的问题,我在react上使用,希望查看从Send verification Email变为Email Verified的组件.我所做的就是创建一个将reload方法包装在用户实例上的函数.这是一个承诺,因此当实现承诺时,我现在调用firebase.auth().currentUser函数,该函数现在会更新重载原因.我将所有这些与组件生命周期等相匹配.

I get the same problem for me, I was using with react and want to see a component who changed from Send verification Email to Email Verified. What I've done it's create a function who wrap the reload method on the user instance. This one is a promise, so when the promise fulfilled I now call the firebase.auth().currentUser function who get now update cause of the reload. I match all that with some component lifecycle etc.

const user = firebase.auth().currentUser;
user.reload().then(() => {
  const refreshUser = firebase.auth().currentUser;
  // do your stuff here
})

重新加载的文档在那里. https://firebase.google.com/docs/reference/js/firebase .User#reload

The docs for the reload are there. https://firebase.google.com/docs/reference/js/firebase.User#reload

希望可以提供帮助:)

这篇关于如何观察firebase.auth().currentUser.displayName和其他属性的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆