How to logout user using Facebook authentication using Swift and iOS? - ios

I've tried searching around but I can't find the answer to my question.
I'm playing around with this application from github:
https://github.com/Yalantis/Koloda/tree/master/Example
I'm using it because I'm doing a project which will use the tile based swiping from this application.
I'm also using firebase. So far, I can add users to my database, so the app and firebase are connected.
I also want users to register/login using facebook.
I have connected my app to facebook and a user can come in and successfully login using facebook authentication.
The problem I am having is logging out.
When I click on the logout button, I want the user to be completely logged out. So there is a logout method that comes with the facebook SDK called logout.
Here are the relevant parts of my code.
import FBSDKLoginKit
#IBAction func handleLogout(sender: AnyObject) {
facebookLogin.logOut()
print("loggedout")
}
So when I click on logout and then click on login again I get this page:
http://imgur.com/owi3zZn
I do not want the user to stay authorized after they have clicked on the logout button. How do I make it so that when I click on the logout button, the user is completely logged out from Facebook and then when they click on the login button they have to re-enter their username/password?
I've tried looking around the webz but can't find a solution to my problem, although I'm sure it's something pretty simple, I hope!
Thank you for your help and sorry if the formatting is poor.

If you to make the user log out from the app itself programmatically, you can check the following code.
let loginView : LoginManager = LoginManager()
loginView.loginBehavior = FBSDKLoginBehavior.Web
This will open the Facebook login popup in your app in which users can login into your app.
And for the logout, you can call:
let manager = LoginManager()
manager.logOut()
This will log out the user from the facebook in the app, after this if you call login method of SDK you will see the login popup again
If you want to clear the profile and token, also call the below code with logout.
FBSDKAccessToken.setCurrentAccessToken(nil)
FBSDKProfile.setCurrentProfile(nil)

When you call the logOut, the user is logged out of your app. As far as logging them out of Facebook, I don't think you can do that, nor would Facebook allow it. The first time a user authorizes you app through Facebook, Facebook adds your app to the list of apps that they are authorized with (they can access that page through Facebook.com). When they logOut and logIn again, they will see the page that you posted a picture of because they already authorized it. The only way for them to reauthorize themselves is to delete the app from their Facebook app page and log in to your app again.

The way the current Facebook SDK handles this seems like a security vulnerability to me. If I borrow a friends iPad and login to an app with Facebook when I logout I should be completely logged out of the app and the SFSafariViewController it used to authenticate me. Whereas right now it still remembers me in the SFSafariViewController using cookies.
To properly logout the user from both the Facebook SDK and SFSafariViewController I do the following:
let fbLoginManager = LoginManager()
fbLoginManager.logOut()
let cookies = HTTPCookieStorage.shared
let facebookCookies = cookies.cookies(for: URL(string: "https://facebook.com/")!)
for cookie in facebookCookies! {
cookies.deleteCookie(cookie )
}
I really dislike this solution but it's the best I've been able to come up with.

User Dan L has the correct answer, in one of my comments he wrote:
Oh, I see what you mean. If you go into the safari app, you can go Facebook.com and logout of your account, login to a different account, and go back into your app. The safari extension the pops up when you call the Facebook login should show the new user that logged in!
This is exactly what I needed. Thank you Dan L.

from your comments i can see what you want.
you can just reset your simulator's content and settings :
Click simulator / Reset Content and Settings

Related

swift facebook sdk logout function not working

I am working on app that needs facebook to login. In other means we are using facebook authentication in our app along with in app login screen.
So now at very first, I was able to login using my facebook account. When i first tried it, I have to provide both user name and my password to get login using my facebook account credentials.
Now after that I want to logOut from facebook account whenever user hit Logout button from our app. Here is what I am doing
AccessToken.current = nil
Profile.current = nil
LoginManager().logOut()
but it is not actually working. When I press again to login back, now I never see that screen where I gave facebook account credentials. its mean LogOut() function is not working.
Please help me this is weird behavior from facebook sdk

Trying to logout of app using Facebook Login iOS SDK in Swift [duplicate]

I've tried searching around but I can't find the answer to my question.
I'm playing around with this application from github:
https://github.com/Yalantis/Koloda/tree/master/Example
I'm using it because I'm doing a project which will use the tile based swiping from this application.
I'm also using firebase. So far, I can add users to my database, so the app and firebase are connected.
I also want users to register/login using facebook.
I have connected my app to facebook and a user can come in and successfully login using facebook authentication.
The problem I am having is logging out.
When I click on the logout button, I want the user to be completely logged out. So there is a logout method that comes with the facebook SDK called logout.
Here are the relevant parts of my code.
import FBSDKLoginKit
#IBAction func handleLogout(sender: AnyObject) {
facebookLogin.logOut()
print("loggedout")
}
So when I click on logout and then click on login again I get this page:
http://imgur.com/owi3zZn
I do not want the user to stay authorized after they have clicked on the logout button. How do I make it so that when I click on the logout button, the user is completely logged out from Facebook and then when they click on the login button they have to re-enter their username/password?
I've tried looking around the webz but can't find a solution to my problem, although I'm sure it's something pretty simple, I hope!
Thank you for your help and sorry if the formatting is poor.
If you to make the user log out from the app itself programmatically, you can check the following code.
let loginView : LoginManager = LoginManager()
loginView.loginBehavior = FBSDKLoginBehavior.Web
This will open the Facebook login popup in your app in which users can login into your app.
And for the logout, you can call:
let manager = LoginManager()
manager.logOut()
This will log out the user from the facebook in the app, after this if you call login method of SDK you will see the login popup again
If you want to clear the profile and token, also call the below code with logout.
FBSDKAccessToken.setCurrentAccessToken(nil)
FBSDKProfile.setCurrentProfile(nil)
When you call the logOut, the user is logged out of your app. As far as logging them out of Facebook, I don't think you can do that, nor would Facebook allow it. The first time a user authorizes you app through Facebook, Facebook adds your app to the list of apps that they are authorized with (they can access that page through Facebook.com). When they logOut and logIn again, they will see the page that you posted a picture of because they already authorized it. The only way for them to reauthorize themselves is to delete the app from their Facebook app page and log in to your app again.
The way the current Facebook SDK handles this seems like a security vulnerability to me. If I borrow a friends iPad and login to an app with Facebook when I logout I should be completely logged out of the app and the SFSafariViewController it used to authenticate me. Whereas right now it still remembers me in the SFSafariViewController using cookies.
To properly logout the user from both the Facebook SDK and SFSafariViewController I do the following:
let fbLoginManager = LoginManager()
fbLoginManager.logOut()
let cookies = HTTPCookieStorage.shared
let facebookCookies = cookies.cookies(for: URL(string: "https://facebook.com/")!)
for cookie in facebookCookies! {
cookies.deleteCookie(cookie )
}
I really dislike this solution but it's the best I've been able to come up with.
User Dan L has the correct answer, in one of my comments he wrote:
Oh, I see what you mean. If you go into the safari app, you can go Facebook.com and logout of your account, login to a different account, and go back into your app. The safari extension the pops up when you call the Facebook login should show the new user that logged in!
This is exactly what I needed. Thank you Dan L.
from your comments i can see what you want.
you can just reset your simulator's content and settings :
Click simulator / Reset Content and Settings

How to log out of Facebook when using a custom login button, on iOS using FBSDKLoginKit?

I am implementing a custom login button in my iOS app, using the FBSDKLoginKit CocoaPod, version 4.8.0. I already have log in working, but I can't figure out how to log out of Facebook. I am already calling logOut on an instance of FBSDKLoginManager, but that only seems to clear the currentAccessToken value. When I tap my login button again, I see a screen that looks like this:
I want to be able to log in as a different Facebook user. How can I achieve this?
In the new version of the Facebook iOS SDK (4.6 and above, I think) the default behaviour of login uses Safari View Controller. So if you are logged into Safari and have already logged into your app using Facebook, it will not ask for your credentials again and show you that screen. If you want to log in as a different user, then you will need to log out of Facebook in Safari in which case the login dialog should ask for your credentials. Also, you can remove the app from your app settings and try to login in which case it will show you the permissions screen. This behaviour of the login dialog probably follows from the best practices where a logout from an app should not cause logout from Facebook itself.

Unable to Login as Different User After Logging out of iOS application

So here is my problem. I log in to my application, then log out of my application, but when I try to log in again, I get the screen in the link below.
Login Screen
As you can see, I don't get the opportunity to login with another user, which is what is intended.
What I tried to do was logout and then clear all the cookies in the logout using these following methods:
#IBAction func logout(sender: AnyObject) {
//Logged out here
let loginManager = FBSDKLoginManager()
loginManager.logOut()
//This is one method I tried
let appDomain = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
//This is another method I tried
for key in NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys {
NSUserDefaults.standardUserDefaults().removeObjectForKey(key)
}
//And this is the last method I tried
var cookie: NSHTTPCookie
var storage: NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies! {
var domainName: String = cookie.domain
var domainRange: Range = domainName.rangeOfString("facebook")
if domainRange.length > 0 {
storage.deleteCookie(cookie)
}
}
}
None of these seemed to solve my problem. The app is in "Development" mode in the Facebook Dev account so it might have something to do with this, but not totally sure. Does someone have any experience with this and know the solution to our problem, as shown in the image above.
The whole goal of Facebook login is to allow quick, seamless login into your Facebook account without having to re-enter credentials.
To achieve this, the Facebook SDK tries to leverage credentials that are already stored on the device, which may include:
login information from the Facebook app
cookies related to Facebook login in Safari (directly or via the SFSafariViewController)
system Facebook accounts
When you logout, you actually only have the Facebook SDK forget the credentials within the app (it clears the token). When you login again, it acts like the first time you did, and if it finds an existing user, it will use that (the Facebook SDK makes the — usually valid — assumption that there is a single person using the device, and they have a single Facebook account).
The current "favorite" path for the Facebook SDK (though that varies with SDK versions, iOS versions, and possibly other parameters) is SFSafariViewController, which shares cookies with Safari, not with your app.
If you want the user to completely log out on the device, they would then have to use the log out link within Facebook in Safari (or an SFSafariViewController).
If you want to do so programatically, you may open the following URL in Safari or an SFSafariViewController:
https://www.facebook.com/logout.php?next=[YourAppURL]&access_token=[ValidAccessToken]
You'll have to use a custom URL scheme to return to your app/exit the SFSafariViewController, though.
If you, as a user, want to see permissions request from Facebook again, you should remove the app from user's profile on Facebook.
When you are at settings screen, find your app and hit the cross button

Facebook Login with another account from iOS app

I'm developing an iOS app that integrates with Facebook SDK. When I make the login for the first time with my facebook account it takes the following steps:
Jump to safari web login
Insert my credentials
After that I authorize the permissions that are requested
And then I go to the main menu of my application.
When I press the logout button, I run the following code:
- (void)logout {
[FBSession.activeSession closeAndClearTokenInformation];
}
With this code I can invalidate the session and clear the access token information. However, when i try to login again, my application jumps to safari web login and my account appears already as logged in (next picture).
If I want to login with another Facebook account, i can't do it. In other words, I would like to follow the steps that I said earlier or have a mechanism like "Not you?" that Facebook official app provides.
Any idea that what I have to do?
You're still logged in through Safari. Log out there (or clear your cache).
wrong, happens when not logged in safari and FB app.

Resources