Sing out Facebook firebase auth completely [duplicate] - android

i´m not able to find any solution that solves my Problem.
I use Firebase and Facebook to sign up to my app.
When I first sign up to my app, i get the following screen:
Facebook First Login-Screen
I sign out from Firebase and Facebook using the following Lines of Code:
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
When I´m trying after that to sign in with Facebook again, I get the following Screen:
Facebook Second Login-Screen
I´m only able to continue with the Account I used before.
I want to come back to the first Screen, where I have to enter E-Mail an Password so I could Sign in with another Facebook Account if I want.
On Stackoverflow I found the following promising Code:
FacebookSdk.sdkInitialize(getApplicationContext());
if (AccessToken.getCurrentAccessToken() != null) {
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
AccessToken.setCurrentAccessToken(null);
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
But this solved also not my Problem.
After that I get the following Screen:
Facebook alternate Second Login-Screen
I would by very thankful for any help and sorry for any English mistakes.

Use below code before you click on facebook login button
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
accessTokenTracker.stopTracking();
DebugLog.infoLog("Token Changed Called");
if (currentAccessToken == null) {
LoginManager.getInstance().logInWithReadPermissions(mActivity, Arrays.asList(permissionList));;
}
}
};
AccessToken.refreshCurrentAccessTokenAsync();
After above code just call login code as mentioned below (use handler to call below code with 1 milliseconds wait time):
LoginManager.getInstance().logInWithReadPermissions(mActivity, Arrays.asList(permissionList));

Related

Facebook android sdk not clear previous login at logout

My app has facebook login integration in it, And i found one issue while re-login, When i tried to logout from facebook and after that re-login i am getting previous user auto login in my app but what i need is to login as new user. Don't Know what's Going wrong
My Logout code is
val parameters = Bundle()
val request = GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions/",
parameters,
HttpMethod.DELETE,
GraphRequest.Callback {
// Insert your code here
LoginManager.getInstance().logOut()
})
request.executeAsync()
You need to initialize the FacebookSdk in the logout activity before calling logOut().
LoginManager.getInstance().logOut();,will work
but dont forgot to put FacebookSdk.sdkInitialize(getApplicationContext()); inside your LogoutActivity onCreate
For Logout from Facebook only need to use:
LoginManager.getInstance().logOut();
In facebook SDK,if you want to completely de-couple, make sure you also remove the app from the user's facebook account. This method disconnects the user completely:
public void logoutFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // user already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
also use this FacebookSdk.sdkInitialize(getApplicationContext()); to initialize the facebook sdk.
Hope it will help you!!

Android Facebook login always ask for user after logout

I am implementing a Facebook login with Facebook SDK on Android.
compile 'com.facebook.android:facebook-android-sdk:4.+'
I'm logging in with the user as
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
..
}
And for log out, I use my own log out button and log out the user programmatically:
LoginManager.getInstance().logOut();
My question is:
After log out, when I click on Continue with Facebook, the previous user information pops up to Continue as XY. I don't want this. I want to ask for email and password again, every time, if somebody wants to log in after log out. How can I do this?
Actually i found the solution. I changed the login behavior for the FB login button, for this i used:
loginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
So every time it pops up the WEB view for login button.
I want to ask for email, password again every time if somebody wants
to log in after log out.
try this in your onCreate()
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
updateWithToken(AccessToken.getCurrentAccessToken()); //add this method
Now in updateWithToken() method logout the user from previous session
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken != null) {
LoginManager.getInstance().logOut();
} else {
}
}
EDIT
if you want to completely disconnect user from facebook login use:
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
To really disconnect from facebook with the android SDk and avoid auto login after you must use
val accessToken = AccessToken.getCurrentAccessToken()
val request = GraphRequest.newDeleteObjectRequest(accessToken, "me/permissions", { response ->
LoginManager.getInstance().logOut() //not really needed i think
})
request.executeAsync()
In response you have a responseCode == 200 if OK

Disable auto log in with Facebook loginbutton

I have an Android application that uses Facebook sdk to log in. To do so, I use the LoginButton widget. The log in process works very well but I got a small problem : when I close the application or reinstall it, it automatically logs in into Facebook (the text on button switches to "Log out"). I don't want to have such behavior : I need that the user clicks on the button to log in every time the application starts. I checked on the internet and it seems this feature is called "single sign on" but I'm not sure about that. I have found several ways to do so but none of them works. I used Facebook SDK 4. This is the part of the code that instantiates the Activity.
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
this.setContentView(R.layout.login_layout);
getSupportActionBar().hide();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build();
mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
/*accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
updateWithToken(newAccessToken);
}
};*/
LoginManager.getInstance().logInWithReadPermissions(LogInActivity.this, Arrays.asList("public_profile", "user_birthday", "email"));
LoginManager.getInstance().setLoginBehavior(LoginBehavior.SUPPRESS_SSO);
fb_login = ((LoginButton) findViewById(R.id.fb_login));
fb_login.setLoginBehavior(LoginBehavior.SUPPRESS_SSO);
fb_login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i("Facebook connect", "Connection success");
}
#Override
public void onCancel() {
Log.i("Facebook", "Super c'et cancel");
((TextView) findViewById(R.id.account)).setText("Annulé");
}
#Override
public void onError(FacebookException error) {
Log.i("Facebook", "Super y a une erreur");
((TextView) findViewById(R.id.account)).setText("Erreur");
}
});
Does someone have an idea how to disable to automatic connection ?
For Facebook SDK 4 and onwards, you could programmatically logout using:
LoginManager.getInstance().logOut();
Now, the problem is where do you place this code. If you're fine with Facebook logout when the app is sent to the background, you can use onPause() or onStop() methods.
The big part comes. You can't determine if the app is killed. Because it's simply killed and you don't get any callbacks to catch that event.
So, if you anyway want to implement this every-time-login thing, you have to do this when the app starts.
Initialize the Facebook SDK:
Check if the user is logged in.
If yes in step 2, run manual logout.
FacebookSdk.sdkInitialize(getApplicationContext());
if(AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance().logOut();
}
step 1: like was written before you should make
LoginManager.getInstance().logOut();
step 2: To remove autologin just avoid behavior of Facebook app. You could do it via LoginBehavior.
LoginManager.getInstance().setLoginBehavior(LoginBehavior.WEB_ONLY);
Remove this line from your code
LoginManager.getInstance().logInWithReadPermissions(LogInActivity.this, Arrays.asList("public_profile", "user_birthday", "email"));

Facebook SDK 4 for Android - how to log out programmatically

Recently, Facebook released SDK 4 with new and cool updates. I tried to switch into SDK4 to use new features, however, I am struggling with the Login feature of Facebook.
So far, to log out Facebook programmatically, I used :
Session session = Session.getActiveSession();
session.closeAndClearTokenInformation();
But SDK4 seems not to support Session anymore, and in official docs, they mention:
There are two ways to implement Facebook login on Android:
LoginButton class - Which provides a button you can add to your UI. It follows the current access token and can log people in and out.
Well, seems there's no way to log out Facebook programmatically except using LoginButton.
Anyone have any idea, please share it here.
You can use LoginManager.getInstance().logOut();, even if you use LoginButton because
This UI element wraps functionality available in the LoginManager.
EDIT:
Just to mention that this works for Facebook SDK v4. I don't know if they will change it in the future.
#as batoutofhell mention, don't forget to put FacebookSdk.sdkInitialize(getApplicationContext()); to initialize the facebook sdk. Please see here for the details.
SDK4, if you want to completely de-couple, make sure you also remove the app from the user's facebook account. This method disconnects the user completely:
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
You can use LoginManager.logOut()
Check out https://developers.facebook.com/docs/reference/android/current/class/LoginManager/
To handle it with the loginButton:
//Check if user is currently logged in
if (AccessToken.getCurrentAccessToken() != null && com.facebook.Profile.getCurrentProfile() != null){
//Logged in so show the login button
fbLogin.setVisibility(View.VISIBLE);
fbLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//log out
LoginManager.getInstance().logOut();
gotoLogin();
}
});
}
You can logout by using LoginManager but you have to use graph request also. I am talking about log out completely so, that next time you can login with different account.
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
SharedPreferences pref = DashBoard.this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
LoginManager.getInstance().logOut();
Intent logoutint = new Intent(DashBoard.this,MainActivity.class);
logoutint.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logoutint);
}
}).executeAsync();
By the help of shared preferences here you can logout completely, and next time you can login with different account.
Frank version kotlin:
fun disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return // already logged out
}
GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions/",
null,
HttpMethod.DELETE,
GraphRequest.Callback {
LoginManager.getInstance().logOut()
}).executeAsync()
}

Facebook Login on Android App

I have seen numerous stackoverflow posts but none of them seem to be helping me in this problem.
When I try to login using facebook, the facebook app opens, asks for permissions and then when I click OK, everything goes into background. There is nothing in the logs.
Things that I have already tried:
I have created app on Facebook developer console
Added Android as platform on Settings page, added package name, class name and generated key hash. Then i also added the key hash from the logcat error with = at the end.
I have added my email address in contact, and made my app go live in Status & Review.
I have installed and logged in to Facebook app on my android device.
I have tried deleting my debug.keystore, and generating new hash
I have tried deleting Facebook developer app and creating it from scratch
I am using LoginManager to sign-in to facebook from my android app.
Here is my code for login:
private void loginWithFacebook() {
final LoginManager loginManager = LoginManager.getInstance();
CallbackManager callbackManager = CallbackManager.Factory.create();
loginManager.registerCallback(
callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
Log.e("WelcomeScreen", "onSuccess");
}
#Override
public void onCancel() {
Log.e("WelcomeScreen", "onCancel");
}
#Override
public void onError(FacebookException e) {
Log.e("WelcomeScreen", "onError");
}
}
);
loginManager.logInWithReadPermissions(this, Arrays.asList("public_profile"));
}
I have already done this in onCreate:
FacebookSdk.sdkInitialize(getApplicationContext());
I figured out the problem. In my manifest, I had specified noHistory to true for the activity where I was expecting the activity result back from Facebook activity, but since the activity is destroyed once it launches another activity, the app was shutting down. Removing the hoHistory resolved the issue.

Categories

Resources