How to observe firebase.auth().currentUser.displayName and other attributes for changes?

ⅰ亾dé卋堺 提交于 2019-12-19 21:45:06

问题


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.

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).

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?

Thanks!


回答1:


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
})

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

Hope that can help :)




回答2:


There aren't any callbacks for when a Firebase Authentication user profile changes. The typical pattern is to write the user profile data into a data store (such as Realtime Database) every time an authentication listener is triggered in a client app. Clients can then listen to the location where profile data for each user lives, and react to those changes on their own. Also, you can use Cloud Functions for Firebase to write a database write trigger that can check to see if anything actually changed when profile data is written, and take action there as needed.



来源:https://stackoverflow.com/questions/43997243/how-to-observe-firebase-auth-currentuser-displayname-and-other-attributes-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!