Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get Firebase Auth User UID?

I am looking to fetch Auth User(s) UID from Firebase via NodeJS or Javascript API. I have attached screenshot for it so that you will have idea what I am looking for.

enter image description here

Hope, you guys help me out with this.

like image 517
Piyush Bansal Avatar asked Jul 13 '16 13:07

Piyush Bansal


People also ask

How can I get user details of Firebase authentication?

If the user login with a custom "email/password" you don't know anything else about that user (apart from the unique user id). If a user login with Facebook, or with Google sign in, you can get other information like the profile picture url. It is explained here: firebase.google.com/docs/auth/android/… .

Is Firebase user uid unique?

Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).

Does Firebase user uid change?

UIDs will never change.


2 Answers

Auth data is asynchronous in Firebase 3. So you need to wait for the event and then you have access to the current logged in user's UID. You won't be able to get the others. It will get called when the app opens too.

You can also render your app only once receiving the event if you prefer, to avoid extra logic in there to determine if the event has fired yet.

You could also trigger route changes from here based on the presence of user, this combined with a check before loading a route is a solid way to ensure only the right people are viewing publicOnly or privateOnly pages.

firebase.auth().onAuthStateChanged((user) => {   if (user) {     // User logged in already or has just logged in.     console.log(user.uid);   } else {     // User not logged in or has just logged out.   } }); 

Within your app you can either save this user object, or get the current user at any time with firebase.auth().currentUser.

https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged

like image 93
Dominic Avatar answered Oct 19 '22 11:10

Dominic


if a user is logged in then the console.log will print out:

if (firebase.auth().currentUser !== null)          console.log("user id: " + firebase.auth().currentUser.uid); 
like image 29
JamesD Avatar answered Oct 19 '22 12:10

JamesD