Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is returned on firebase.auth().onAuthStateChanged?

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?

like image 440
Noman Avatar asked Jan 01 '26 00:01

Noman


1 Answers

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.

like image 93
James Daniels Avatar answered Jan 04 '26 22:01

James Daniels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!