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);
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);
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