Facebook and Google Authentication in Firebase - Android Studio

Asked

Viewed 924 times

0

I’m developing an Android app, with Android Studio, using Firebase initially for user authentication.

The code is quite extensive, so I won’t copy it here, but it’s available on Github.

Already implemented the login with Google, the user logs, goes to the home screen where you choose a room and, when entering the room, appears a chat window where you can send messages, including already with Google photo and profile name.

I am now implementing the login with Facebook according to the tutorials of Firebase and of Facebook, I have already followed all the configuration steps in Developers.facebook.com, such as app id, key hash, etc., in addition to creating the login button with Facebook.

The login is practically functional, the user enters with his account through the standard Facebook window, but when finishing the login, the application goes back to the home screen. That is, it takes the information but does not go to the next Activity (the Mainactivity, where the rooms are).

Me and the other developers of the app are students and beginners in Android programming, so I believe it is something simple but we are not able to fix it. Because we are beginners I also ask that if someone responds, please explain in which file would be the modification, the Facebook tutorial is vague on this. Thanks in advance.

1 answer

1


//This method is called in the onClick of the face boot

private void loginFacebookResult() {     
buttonFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            handlerFacebookAccessToken(loginResult.getAccessToken());
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }

    });
}  
private void handlerFacebookAccessToken(AccessToken accessToken) {

    AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
    mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {

            showProgressDialog();

            if (!task.isSuccessful()) {
                Log.w("Facebook Message:", "signInWithCredential", task.getException());
                Toast.makeText(LoginActivity.this, "Erro de Login", Toast.LENGTH_SHORT).show();
            } else {
                Usuario user = new Usuario();
                user.setId(mAuth.getCurrentUser().getUid());
                user.setNome(mAuth.getCurrentUser().getDisplayName());
                user.setEmail(mAuth.getCurrentUser().getEmail());

user.setUrl(mAuth.getCurrentUser().getPhotoUrl().toString()); user saveBD();

                Intent intentMain = new Intent(LoginActivity.this, (sua classe destino).class);
                startActivity(intentMain);
            }

            hideProgressDialog();
        }
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.