Facebook Login With Firebase In Android JavaAndroid Development
November 20, 20201 minuteuserPankaj Valani
Facebook Login With Firebase In Android Java
Facebook Login With Firebase In Android Java

1.Add this library build.gradle(:app)

  • This library user for firebase integration dependencies 
  • {
    implementation 'com.google.firebase:firebase-auth:19.4.0'
    }
2.Add this permission in AndroidManifest.xml

  • This permission use internet connection checking
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
3.Below Facebook Button Add on activity xml :

<com.facebook.login.widget.LoginButton
         android:id="@+id/login_button"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_gravity="center"
         android:visibility="gone" />
4.Activity in add this permission

  • This permission user for user data get. Without this permission user data not meet

  • loginButton” This button is Facebook button 

  • LoginManager.getInstance().logInWithReadPermissions(FacebookLoginActivity.this, Arrays.asList("email", "public_profile")); 
  • loginButton.setReadPermissions("email", "public_profile");
5.Initialize Authentication:

void initializeAuthentication(){
                FirebaseAuth  mAuth = FirebaseAuth.getInstance();
             GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
             .requestIdToken(getString(R.string.default_web_client_id))
             .requestEmail()
             .build();
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
6.Add This Function:

  • This function user login result return
void faceBookLogin(LoginResult loginResult){
     setFacebookData(loginResult);
     Profile profile = Profile.getCurrentProfile();
     if (profile != null) {
       String avatar =  ImageRequest.getProfilePictureUri(profile.getId(), 200, 200).toString();
   }
   handleFacebookAccessToken(loginResult.getAccessToken());
}
7.Handle Token :

  • This token return firebase login fails or not an event
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();
                       user.getIdToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
                           @Override
                           public void onComplete(@NonNull Task<GetTokenResult> task) {
                               String token = task.getResult().getToken();

                           }
                       });

                   } else {
                      String errorCode = String.valueOf(task.getException());
                       Toast.makeText(FacebookLoginActivity.this, "Login failed.", Toast.LENGTH_SHORT).show();
                    } 
            }
    });
}
Notes:

  • “Default_web_client_id” this keyword, not changes because creating google-services.json file time automatically this keyword in add values so this key work put at its
How to creating google JSON file : JSON File

Add google JSON file this location in android studio: