0

Can you help in my Android project. I am trying to get the the currentUID in firebase auth and I want it to show the string value in the document of my firestore.

db2.collection("Assessment").document(??).set(answer);
theduck
  • 2,589
  • 13
  • 17
  • 23
  • 1
    Possible duplicate of [Mapping Firebase Auth users to Firestore Documents](https://stackoverflow.com/questions/46622291/mapping-firebase-auth-users-to-firestore-documents) – Edric Nov 10 '19 at 05:33

1 Answers1

1

The currently logged-in user's ID can be retrieved via the getUid method of a FirebaseUser instance, which can be retrieved via FirebaseAuth#getCurrentUser:

Java:

FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
String uid = user.getUid();
// Or alternatively, a one-liner (although the code complexity is higher)
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

Kotlin:

val auth = FirebaseAuth.getInstance()
val user = auth.currentUser // Note: This returns a nullable class
val uid = user?.uid
// Or alternatively, a one-liner (although the code complexity is higher)
val uid = FirebaseAuth.getInstance().currentUser?.uid
Edric
  • 24,639
  • 13
  • 81
  • 91