exportdefaultfunction(){// our reactive properties...letstate=reactive({user:null,loading:true,error:null});// make the firebase call to listen for change in auth state,// we have set initial loading status to true so nothing happens on UI // side until loading is set to falsefirebase.auth().onAuthStateChanged(_user=>{if(_user){state.user=_user;}else{state.user=null;}state.loading=false;});// return all of the properties from the functionreturn{...toRefs(state)};}
Every time you call const { user } = useAuth() in one of your components it will execute this part: firebase.auth().onAuthStateChanged....
What if you need to access user in multiple components?
completely valid point, the event registration should be executed only once in the app. also the state should be kept outside the hook function, in such a way it is shared over components initiating the hook.
The way how the state is exposed now makes the state mutable outside the hook function. If the state is exposed as a computed, it becomes unmutable, that's cleaner.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I mean in your
use/auth.js
Every time you call
const { user } = useAuth()
in one of your components it will execute this part:firebase.auth().onAuthStateChanged...
.What if you need to access
user
in multiple components?Btw, have you checked my implementation?
completely valid point, the event registration should be executed only once in the app. also the state should be kept outside the hook function, in such a way it is shared over components initiating the hook.
The way how the state is exposed now makes the state mutable outside the hook function. If the state is exposed as a computed, it becomes unmutable, that's cleaner.