Firebase: How to Get User.Uid

Is there any way to get Firebase Auth User UID?

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

How to get the current user id from Firebase in Flutter

Update (2020.09.09)

After firebase_auth version 0.18.0

Few breaking updates were made in firebase_auth 0.18.0. FirebaseUser is now called User, currentUser is a getter, and currentUser is synchronous.

This makes the code for getting uid like this:

final FirebaseAuth auth = FirebaseAuth.instance;

void inputData() {
final User user = auth.currentUser;
final uid = user.uid;
// here you write the codes to input the data into firestore
}

Before firebase_auth version 0.18.0

uid is a property of FirebaseUser object. Since auth.currentUser() return a future, you have to await in order to get the user object like this:

final FirebaseAuth auth = FirebaseAuth.instance;

Future<void> inputData() async {
final FirebaseUser user = await auth.currentUser();
final uid = user.uid;
// here you write the codes to input the data into firestore
}

firebase v9 I am storing the User UID from firebase auth into firestore, help me retrieve it for sign in comparison

If you don't need to get all user docs, but just the one for the current user, that'd be:

async function getUser() {
const ref = doc(db, "users", FirebaseAuth.instance.currentUser.uid));
const userDoc = await getDoc(ref);
return userDoc.data();
}
getUser();

Get user id after creating a user in firebase

You can try this:

firebase.auth().createUserWithEmailAndPassword(email, password)
.then((userCredential) => { // the userCredential is a variable that will hold the response in it, it contains all the user info in it
// Signed in
var user = userCredential.user;
// This user variable contains all the info related to user including its id
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
});

Reference

How to get the current UserId from Firestore?

The uid in your code is typically the unique identified of the user. Firestore itself has no knowledge of such a uid. Instead, the uid comes from the identity provider that you use.

You could use the user ID from Google Sign-in to identify the user in Firestore, but it's more common to sign in with Firebase (too).

Once you've signed in to Google, you can use those credentials to sign in to Firebase Authentication by calling signInWithCredential as shown in the documentation on social authentication.

Once you're signed in to Firebase Authentication you'll get a UID from Firebase Authentication, which you can then use to identify the user's documents in Firestore (and later also secure access to those documents).

Once you've done that, you can get the current user (and from there their UID) through FirebaseAuth.instance.currentUser (once) or FirebaseAuth.instance.currentUser() (continuous) and then again use that in your Firestore calls.

How to get the UID Authentication from Firebase in Flutter and save in Firestore

When you use createUserWithEmailAndPassword it will return a UserCredential as a result, you can use that to update your user variable, so in code it would be something like this:

UserCredential result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password);
User? user = result.user;

How to get Firebase UID knowing email user?

There is no way to look up a user's UID from their email address using the Firebase Authentication client-side APIs. Since this lookup is considered a trusted operations, it is only available in the Admin SDK for Firebase Authentication.

The two most common solutions are:

  1. Create a custom server-side API in a trusted environment (such as Cloud Functions) that performs the lookup, and then call that API from your client-side application. You will have to make sure that only authorized users can perform this lookup.

  2. Store the information about each user into a database (like the Realtime Database that you tagged your question with) when their account is created, or whenever they sign in. Then you can look up the UID from the email in the database. Here too, you will have to ensure that the data is only available in ways that fit with your application's data privacy requirements.

Note that if you just need to know whether an email address is in use (and not the specific UID that uses it), you can call the fetchSignInMethodsForEmail method.

firebase get User UID using phone number

Yes, you need the Firebase Admin SDK.

admin.auth().getUserByPhoneNumber("+213666666666")
.then((userRecord) => console.log(userRecord))
.catch((error) => console.error(error));


Related Topics



Leave a reply



Submit