15

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:

https://i.stack.imgur.com/8lQWv.jpg

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.

Gaurav Patel
  • 532
  • 1
  • 5
  • 19
Joe
  • 183
  • 1
  • 2
  • 6

5 Answers5

24

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)
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
HardikDG
  • 5,892
  • 2
  • 26
  • 55
13

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.

Dan Levy
  • 3,931
  • 4
  • 28
  • 48
  • Darn that's a bummer because I want to log in with different facebook accounts to see if my database gets updated correctly. Would you know anyway around this? Thanks! – Joe Apr 02 '16 at 03:18
  • 4
    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! – Dan Levy Apr 02 '16 at 03:21
  • Genius. Thank you, that works perfectly! If I had enough rep you would get my upvote! – Joe Apr 02 '16 at 03:51
  • 1
    It is not user friendly for user to logout themselves in Safari. – chengsam Oct 04 '17 at 04:25
  • Dan L's answer is not working for me. Still logged in with the wrong account when opening the web view from within my app.. – Nicolai Harbo Apr 20 '20 at 08:46
7

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.

Surbhi Garg
  • 414
  • 5
  • 19
ToddH
  • 2,801
  • 2
  • 28
  • 28
2

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.

Community
  • 1
  • 1
Joe
  • 183
  • 1
  • 2
  • 6
2

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

Dory Daniel
  • 798
  • 14
  • 21