Android Development

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
				
					
{
<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"/>
}
				
			

3.Activity in add this permission

  • This permission is for user data. 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”);

4.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, so); 
}
				
			

5.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());
}
				
			

6.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() {                
      @Override                
      public void onComplete(@NonNull Task task) {
      if (task.isSuccessful()) {
      FirebaseUser user = mAuth.getCurrentUser();
      user.getIdToken(true).addOnCompleteListener(new OnCompleteListener() {
      @Override
      public void onComplete(@NonNull Task 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:

1
Share On: