Facebook authentication without login button
@erdomester, @sromku
Facebook launch new sdk version 4.x where Session is deprecated,
There new concept of login as from facebook
LoginManager and AccessToken - These new classes perform Facebook
Login
So, Now you can access Facebook authentication without login button as
layout.xml
<Button
android:id="@+id/btn_fb_login"
.../>
MainActivity.java
private CallbackManager mCallbackManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
setContentView(R.layout.activity_main);
Button btn_fb_login = (Button)findViewById(R.id.btn_fb_login);
btn_fb_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
}
});
}
Edit
If you don't add the following, it won't work (rightly pointed out by @Daniel Zolnai in comment below):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
How to make facebook login button required to click?
Don't use FB.Event.subscribe()
if you don't want to auto-login the user.
Instead, just create a normal button <button></button>
and on its click perform actions you want using FB.getLoginstatus()
and FB.login()
. So, on click of this button do this:
$( "#my_fb_button" ).click(function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
setField();
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
login();
} else {
// the user isn't logged in to Facebook.
login();
}
});
});
function login()
{
FB.login(function(response) {
if (response.authResponse) {
setField();
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email,user_birthday'});
}
Hope the flow is clear now. Many apps use this flow too and it works like charm. Good luck!
Firebase facebook button android authentication when logged out
You have to log out from the Facebook SDK too, using the LoginManager class (com.facebook.login.LoginManager).
These two lines work for me:
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
Hope this helps :)
Facebook Login without FBSDKLoginButton in Swift
FBSDKLoginManager can be used if FB Login is handled via custom button.
For more info, your can check this.
Related Topics
How to Compile C into an Executable Binary File and Run It in Android from Android Shell
How to Fix "Failed to Sync Vcpu Reg" Error
How to Tell If a Closed Path Contains a Given Point
How to Update Notification with Remoteviews
Voice Recognition Stops Listening After a Few Seconds
How to Play Audio File in Android
How to Have a Gridview That Adapts Its Height When Items Are Added
How to Display Menu Item with Icon and Text in Appcompatactivity
Broadcast Receiver Class and Registerreceiver Method
Adding a Color Filter to a Drawable Changes All Buttons Using the Same Drawable
Android: How to Convert Whole Imageview to Bitmap
How to Use Android Emulator for Testing Bluetooth Application
Android and Setting Alpha for (Image) View Alpha
Cordova Plugin Development - Adding Aar
On Android 8.1 API 27 Notification Does Not Display
Performing Action After Fragment Transaction Animation Is Finished
Add Google Maps API V2 in a Fragment
Preferencefragmentcompat Has Padding on Preferencecategory That I Can't Get Rid Of