Reputation: 305
I am trying to get the email address of the current user. I have the following code in my onCreate, but it isn't working:
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
userEmail = firebaseUser.getEmail();
}
}
};
Note that variable authListener at the top is "never used" according to my program. Not sure if this has anything to do with it. Does anyone know what could be wrong with the code I have above?
Upvotes: 2
Views: 5081
Reputation: 305
Update, got it working! The below code gets the current users email.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String userEmail = user.getEmail();
} else {
// No user is signed in
}
Upvotes: 2