Sometimes, we want to get Firebase Auth User UID with JavaScript.
In this article, we’ll look at how to get Firebase Auth User UID with JavaScript.
How to get Firebase Auth User UID with JavaScript?
To get Firebase Auth User UID with JavaScript, we call onAuthStateChanged.
For instance, we write
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// ...
console.log(user.uid);
} else {
// ...
}
});
to call onAuthStateChanged with a callback that gets the user.
And we get the user.uid if user is set, which means the user is logged in.
Conclusion
To get Firebase Auth User UID with JavaScript, we call onAuthStateChanged.