Facebook login with Firebase for Android does not show a popup window with options to log in with a different account

Viewed 554

I'm working on an Android app that requires login either using Google or Facebook, via Firebase. I realised that the option for selecting an account works on the first time after installation, i.e. the dialog window asking for account selection does not show up after logging in for the first time. The next time you press a login button, it directly logs in with your previous FB/ Google credentials. This is the GoogleSignInClient.

This is the Facebook Login Manager (not sure if it's actually that, but yeah you get the point).

These dialog windows for login using the respective domains pop up only for the first time after installation of the app. I found the solution for Google's Sign In Client (signing out the client every time the app opens using googleSignInClient.signOut(), but I still haven't found a similar solution for Facebook's Login Manager.

Here's my code: LoginActivity.class

    @Override
    protected void onStart(){
        super.onStart();
        firebaseAuth.addAuthStateListener(authStateListener);
        // FirebaseUser currentUser = firebaseAuth.getCurrentUser();
    }

    @Override
    protected void onStop(){
        super.onStop();
        if (authStateListener != null){
            firebaseAuth.removeAuthStateListener(authStateListener);
        }
    }

signInFBBtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                handleFacebookToken(loginResult.getAccessToken());
            }

            @Override
            public void onCancel() {
                Toast.makeText(LoginActivity.this, "Cancelled login. (Facebook)", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(LoginActivity.this, "Failed to connect to Facebook. Try again.", Toast.LENGTH_SHORT).show();
            }
        });


        authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null){
                    updateUI(user);
                }
                else{
                    updateUI(null);
                }
            }
        };

        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                if(currentAccessToken == null){
                    firebaseAuth.signOut();
                }
            }
        };
    }
@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN){
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        if (task.isSuccessful()){
            handleSignInResult(task);
        }
        else{
            Toast.makeText(LoginActivity.this, "Cancelled Google Login.", Toast.LENGTH_SHORT).show();
            return;
        }
        // Toast.makeText(LoginActivity.this, "Entered onActivityResult's if block.", Toast.LENGTH_SHORT).show();
    }
    }

private void handleFacebookToken(AccessToken token){
        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                    Toast.makeText(LoginActivity.this, "Signed In. (Facebook)", Toast.LENGTH_SHORT).show();
                    updateUI(user);
                }
                else{
                    Toast.makeText(LoginActivity.this, "Couldn't connect to Facebook. Try Again.", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }

I'd appreciate help on the Facebook front.

P.S.:

  1. I've added code relevant only to the FB part; I've omitted the generic Android stuff.
  2. Please don't mind the indents, this is my first ever question on StackOverflow and I didn't want to take time out for markup :P
0 Answers
Related