Categories
JavaScript Answers

How to get Firebase Auth User UID with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *