swift facebook sdk logout function not working - ios

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

Related

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

iOS 9 Facebook SDK Multiple Users

I have followed the iOS 9 Facebook SDK instruction to the letter and facebook login works great, but the app can only support one Facebook user.
I want to support multiple users, but seem to be stuck with whatever user I first logged in with through the Facebook SDK.
Scenario 1:
If I delete my app from the list of allowed apps on facebook website (in settings) and then activate the Facebook login (provided by the Facebook SDK) in my app, rather than allowing me to login in as a different user it asks if I want to "Continue" loging in as the last user that used the facebook login from my App. If I confirm it reauthorizes the app on my facebook account and logs me in as the last user that logged in. Here is the dialog I get when trying to login in after deleting the app from my facebook account.
Scenario 2: The app has been authorized on my facebook account. I chose to log out in my app. I call the facebook logout as shown
FBSDKLoginManager().logOut()
When I go to log in again using the facebook login button (provided by the Facebook sdk), rather than present me with a new facebook login screen it gives me the following:
I don't want to confirm login as the last user. Hitting cancel does nothing. Hitting confirm logs me in again as the last user.
Question: How do I force the facebook sdk to clear out the last users facebook credentials?
Thanks in advance!!
Logout, than set loginBehavior = FBSDKLoginBehaviorWeb when you login.
Reset Facebook Token Reference - Facebook SDK 4.0
When you set login behaviour to web:
loginManager.loginBehavior = .web
with every login you can auth with new credentials

Logout from Facebook ios sdk objective c

i am implementing Facebook login in my ios app. Once i logout and try to login again it don't ask me the User email and password, instead it directly show a permission screen. How to make it ask the user email and password again. I have tried to logout by using login manager logout method and even setting current access token to nil, but it don't work. Thanks in advance for your answer.
You have to logout from your mobile brower or Simulator browser for it.
Open Browser, Open Facebook.com and logout from your account.
If you are not logged into from your browser then It will ask for email and password.
This will solve your issue.

How to logout user using Facebook authentication using Swift and 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

Authentication and login to my app with Facebook iOS SDK

I am using the Facebook iOS SDK to allow quick sign-up of users in my app. Here is the flow:
User taps "Login with Facebook" button
Taken to Facebook app or Safari for login
Redirected back to my app
Show Sign-up for with pre-filled fields from FBGraphUser object
User creates account using data from Facebook
After they created an account, is it possible to authenticate with Facebook, and let them login to my app without having to enter the username and password they just created? How would the flow of that work?
Seems like I should be able to do the following:
User taps on "Login with Facebook" button
Authenticate with Facebook SDK
Return to app, and make a call to server, checking to see if there is a username matching the Facebook email
Allow the user into the app with no password, since they have authenticated with Facebook and have an account in our system.
Is this the correct way to do it?
That is very close. However, it is not enough to just check that the email/username is valid. It is strongly recommended to validate the token directly with Facebook as well.
See Use Facebook authentication token in API for a very similar concept and the Facebook docs about verifying token: https://developers.facebook.com/docs/graph-api/securing-requests.

Resources