Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a Firebase user is signed in using email and password authentication

I'm using Firebase authentication. The user can log in via e-mail password, google and facebook. How do I know if a user is logged in using an email-password? I looked at this answer

and i tried this:

for (UserInfo user: FirebaseAuth.getInstance().getCurrentUser().getProviderData()) {
    if (!(user.getProviderId().equals("facebook.com") || user.getProviderId().equals("google.com"))) {
        System.out.println("User is signed in with Email");
    }
}

But when I use both google and email passwords if statement being true.

like image 658
Onur Çeker Avatar asked Aug 17 '17 13:08

Onur Çeker


2 Answers

The provider ID for email+password is password. So:

for (UserInfo user: FirebaseAuth.getInstance().getCurrentUser().getProviderData()) {
    if (user.getProviderId().equals("password")) {
        System.out.println("User is signed in with email/password");
    }
}
like image 113
Frank van Puffelen Avatar answered Sep 24 '22 19:09

Frank van Puffelen


Below is what works for me:

user.getProvider() returns firebase

user.getProviderData() returns some Id.

The list returned by user.getProviders() returns the login account type

Here's what I did:

@Override
    protected void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.

        FirebaseUser currentUser = mAuth.getCurrentUser();
        if (currentUser == null)
            return;

        for (int i = 0; i < currentUser.getProviders().size(); i++) {
            if (!currentUser.getProviders().get(i).equals("google.com") && !currentUser.getProviders().get(i).equals("facebook.com")) {
               //User signed in with a custom account
               //Todo: call server DB or perform some task

            } else
               updateUI(currentUser);
        }

       // Todo: Complete login activity and switch to the main activity
}
like image 25
R.K Avatar answered Sep 26 '22 19:09

R.K