Search code examples
androidfacebookfirebasefirebase-authenticationfacebook-authentication

Logging in with FireBase facebook authentication, getCurrentUser() keeps returning null


I am struggling right now to pass on the CurrentUser when I am logging in with Facebook (with firebase authentication). It has been working before, but as I log into my application now, the currentuser keeps returning null.

I think it might be because I am already logged into the app with Facebook, but the firebase authentication sees me as a new user. However, I am not sure.

My onStart on the LogIn page looks the following:

@Override
public void onStart() {
    super.onStart();

    // Check if user is signed in (non-null) and update UI accordingly.
    mAuth.addAuthStateListener(mAuthListener);
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null && AccessToken.getCurrentAccessToken() != null) {
        Log.d(TAG, "onComplete: We run this 2" + currentUser + mAuth);
        ItemHolder.getInstance().setUser(mAuth.getCurrentUser());
        updateUI();
    }
    else{
        Log.d(TAG, "onComplete: else run in onstart");
    }
}

The ItemHolder object is a singleton used to store the information of the User. However, it never reaches that, because the user is always null.

I have added an AuthStateListener, and it says that there is no currentuser. My problem is that I have no idea how to retrieve the currentUser when it keeps returning null? Everything else in my login activity follows the firebase quick guide provided, to login with Facebook.


Solution

  • Okay I fixed my problem.

    So I tried to add the currentUser to actually be apart of my updateUi() method. Then it seemed my problem was, that I updated the UI in my facebookCallback(), which seemed to change the activity before actually handling the login.

    So now my code looks the following:

    private void facebookCallback() {
        FBLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "onComplete: onSuccess");
                handleFacebookAccessToken(loginResult.getAccessToken());
            }
    

    And

    private void handleFacebookAccessToken(AccessToken token) {
        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            FirebaseUser user = mAuth.getCurrentUser();
                            Log.d(TAG, "onComplete: We run this");
                            updateUI(user);
                        }
                        else{
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(LogInActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }