Authenticate Using Facebook Login on Android


Verify Using Facebook Login on Android


You can allow your clients to validate with Firebase utilizing their Facebook accounts by coordinating Facebook Login into your application.


Before you start


On the off chance that you haven't as of now, add Firebase to your Android project.


On the Facebook for Developers site, get the App ID and an App Secret for your application.


Empower Facebook Login:


In the Firebase support, open the Auth segment.


On the Sign in strategy tab, empower the Facebook sign-in technique and indicate the App ID and App Secret you got from Facebook.


At that point, ensure your OAuth divert URI (for example my-application 12345.firebaseapp.com/__/auth/controller) is recorded as one of your OAuth divert URIs in your Facebook application's settings page on the Facebook for Developers site in the Product Settings > Facebook Login config.


Utilizing the Firebase Android BoM, pronounce the reliance for the Firebase Authentication Android library in your module (application level) Gradle record (typically application/build.gradle).


Java


Kotlin+KTX


conditions {


/Import the BoM for the Firebase stage


usage platform('com.google.firebase:firebase-bom:26.4.0')


/Declare the reliance for the Firebase Authentication library


/When utilizing the BoM, you don't indicate variants in Firebase library conditions


usage 'com.google.firebase:firebase-auth'


}


By utilizing the Firebase Android BoM, your application will consistently utilize viable renditions of the Firebase Android libraries.


(Elective) Declare Firebase library conditions without utilizing the BoM


Validate with Firebase


Coordinate Facebook Login into your application by following the engineer's documentation. At the point when you design the LoginButton or LoginManager object, demand the public_profile and email authorizations. In the event that you incorporated Facebook Login utilizing a LoginButton, your sign-in action has code like the accompanying:


Java


Kotlin+KTX


/Initialize Facebook Login button


mCallbackManager = CallbackManager.Factory.create();


loginButton = mBinding.buttonFacebookLogin;


loginButton.setReadPermissions("email", "public_profile");


loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {


@Override


public void onSuccess(LoginResult loginResult) {


Log.d(TAG, "facebook:onSuccess:" + loginResult);


handleFacebookAccessToken(loginResult.getAccessToken());


}


@Override


public void onCancel() {


Log.d(TAG, "facebook:onCancel");


/...


}


@Override


public void onError(FacebookException blunder) {


Log.d(TAG, "facebook:onError", mistake);


/...


}


});


/...


@Override


secured void onActivityResult(int requestCode, int resultCode, Intent information) {


super.onActivityResult(requestCode, resultCode, information);


/Pass the movement result back to the Facebook SDK


mCallbackManager.onActivityResult(requestCode, resultCode, information);


}


In your sign-in action's onCreate technique, get the shared case of the FirebaseAuth object:


Java


Kotlin+KTX


private FirebaseAuth mAuth;


/...


/Initialize Firebase Auth


mAuth = FirebaseAuth.getInstance();


While introducing your Activity, verify whether the client is presently endorsed in:


Java


Kotlin+KTX


@Override


public void onStart() {


super.onStart();


/Check if client is endorsed in (non-invalid) and update UI in like manner.


FirebaseUser currentUser = mAuth.getCurrentUser();


updateUI(currentUser);


}


After a client effectively signs in, in the LoginButton's onSuccess callback technique, get an entrance token for the endorsed in client, trade it for a Firebase certification, and validate with Firebase utilizing the Firebase qualification:


Java


Kotlin+KTX


private void handleFacebookAccessToken(AccessToken token) {


Log.d(TAG, "handleFacebookAccessToken:" + token);


AuthCredential qualification = FacebookAuthProvider.getCredential(token.getToken());


mAuth.signInWithCredential(credential)


.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {


@Override


public void onComplete(@NonNull Task<AuthResult> task) {


on the off chance that (task.isSuccessful()) {


/Sign in progress, update UI with the endorsed in client's data


Log.d(TAG, "signInWithCredential:success");


FirebaseUser client = mAuth.getCurrentUser();


updateUI(user);


} else {


/If sign in comes up short, show a message to the client.


Log.w(TAG, "signInWithCredential:failure", task.getException());


Toast.makeText(FacebookLoginActivity.this, "Confirmation fizzled.",


Toast.LENGTH_SHORT).show();


updateUI(null);


}


/...


}


});


}


In the event that the call to signInWithCredential succeeds, you can utilize the getCurrentUser technique to get the client's record information.


Subsequent stages


After a client signs in unexpectedly, another client account is made and connected to the accreditations—that is, the client name and secret phrase, telephone number, or auth supplier data—the client endorsed in with. This new record is put away as a feature of your Firebase project, and can be utilized to recognize a client across each application in your venture, paying little mind to how the client signs in.


In your applications, you can get the client's fundamental profile data from the FirebaseUser object. See Manage Users.


In your Firebase Realtime Database and Cloud Storage Security Rules, you can get the endorsed in client's extraordinary client ID from the auth variable, and use it to control what information a client can get to.


You can permit clients to sign in to your application utilizing numerous validation suppliers by connecting auth supplier qualifications to a current client account.


To sign out a client, call signOut:


Java


Kotlin+KTX


FirebaseAuth.getInstance().signOut();