Facebook Authentication Without Login Button

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



Leave a reply



Submit