When I hover over onAuthStateChanged it shows this:
(method) firebase.auth.Auth.onAuthStateChanged(nextOrObserver: firebase.Observer<any, any> | ((a: firebase.User) => any), error?: (a: firebase.auth.Error) => any, completed?: firebase.Unsubscribe): firebase.Unsubscribe
It also seems to work if I do user: firebase.User
firebase.auth().onAuthStateChanged((user) => {
console.log("data from stateChanged: ", user);
})
But when I try to access the properties of user, i.e. user.uid then I get error basically saying inaccessible. So what exactly is the return type here and how do I access that?
onAuthStateChanged returns either a firebase.User or null. You need to null guard it and potentially cast as a firebase.User in Typescript before accessing it's attributes.
I'd recommend using the AngularFire2 and it's AngularFireAuth module, if you're using Angular; we've smoothed over some of these API concerns and have wrapped the callbacks in RxJS Observables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With