paines99
paines99

Reputation: 1

Would it be okay to put onSnapshot() inside an onAuthStateChanged()?

I have one collection of users with some data in firestore (with their uid as the key). After login, first thing I want to do is show the user data and the user being able to modify it, but if onSnapshot execute outside onAuthStateChanged before I retrieve the user uid to get the doc ref, it will fail the first time which causes my code to stop executing. For some reason that error is not caught, so is it good practice to put it in onAuthStateChanged to make sure that I get the user uid before the first time onSnapshot is executed?

Upvotes: 0

Views: 122

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

If your onSnapshot listener depends on the current user, then it's idiomatic to attach that listener on an onAuthStateChanged handler when the user becomes not null, and subsequently detach it there when the user becomes null again.

Upvotes: 2

Related Questions