Facebook Oauth Logout
Asked Answered
M

13

71

I have an application that integrates with Facebook using Oauth 2.

I can authorize with FB and query their REST and Graph APIs perfectly well, but when I authorize an active browser session is created with FB. I can then log-out of my application just fine, but the session with FB persists, so if anyone else uses the browser they will see the previous users FB account (unless the previous user manually logs out of FB also).

The steps I take to authorize are:

  1. Call [LINK: graph.facebook.com/oauth/authorize?client_id...]

This step opens a Facebook login/connect window if the user's browser doesn't already have an active FB session. Once they log-in to facebook they redirect to my site with a code I can exchange for an oauth token.

  1. Call [LINK: graph.facebook.com/oauth/access_token?client_id..] with the code from (1)

Now I have an Oauth Token, and the user's browser is logged into my site, and into FB.

  1. I call a bunch of APIs to do stuff: i.e. [LINK: graph.facebook.com/me?access_token=..]

Lets say my user wants to log out of my site. The FB terms and conditions demand that I perform Single Sign Off, so when the user logs out of my site, they also are logged out of Facebook. There are arguments that this is a bit daft, but I'm happy to comply if there is any way of actually achieving that.

I have seen suggestions that:

A. I use the Javascript API to logout: FB.Connect.logout(). Well I tried using that, but it didn't work, and I'm not sure exactly how it could, as I don't use the Javascript API in any way on my site. The session isn't maintained or created by the Javascript API so I'm not sure how it's supposed to expire it either.

B. Use [LINK: facebook.com/logout.php]. This was suggested by an admin in the Facebook forums some time ago. The example given related to the old way of getting FB sessions (non-oauth) so I don't think I can apply it in my case.

C. Use the old REST api expireSession or revokeAuthorization. I tried both of these and while they do expire the Oauth token they don't invalidate the session that the browser is currently using so it has no effect, the user is not logged out of Facebook.

I'm really at a bit of a loose end, the Facebook documentation is patchy, ambiguous and pretty poor. The support on the forums is non-existant, at the moment I can't even log in to the facebook forum, and aside from that, their own FB Connect integration doesn't even work on the forum itself. Doesn't inspire much confidence.

Ta for any help you can offer. Derek

ps. Had to change HTTPS to LINK, not enough karma to post links which is probably fair enough.

Marylnmarylou answered 4/5, 2010 at 10:22 Comment(5)
Sean's got a point, which you should listen to, because he's got more reputation than you, and that makes him right. If i'm logged into Facebook, then i come to your site, use it (or perhaps just decide to leave in abject disappointment), then log out, i'd be surprised and perhaps even a little outraged if i found i'd been logged out of Facebook too. If the UI made it clear that would happen, fine, but if it's just a button in the top-right corner of the page that says "Logout", maybe with a facebook logo kind of floating nearby, that's fairly surprising. Surprise is not a feature of good UI.Bucko
@Tom: developers.facebook.com/policy Line #6.Nonmetal
@DigitalPrecision: An excellent link, which goes to show that not even Facebook can get this right.Bucko
Agreed. We had to jump through hoops to get it to work within our application. Docs are horrid.Nonmetal
I have the exact opposite problem: When logging out of my selfmade app, I'm instantly logged out out of facebook too.Reverberator
W
45

I was having the same problem. I also login using oauth (I am using RubyOnRails), but for logout, I do it with JavaScript using a link like this:

<a href="/logout" onclick="FB.logout();">Logout</a> 

This first calls the onclick function and performs a logout on facebook, and then the normal /logout function of my site is called.

Though I would prefer a serverside solution as well, but at least it does what I want, it logs me out on both sites.

I am also quite new to the Facebook integration stuff and played around the first time with it, but my general feeling is that the documentation is pretty spread all over the place with lots of outdated stuff.

Whitcher answered 10/5, 2010 at 6:3 Comment(3)
Thanks Christoph! I've tried that and it does indeed work. I had previously been trying to use FB.Connect.logout and FB.Connect.logoutAndRedirect, which I think are from an older version of the API. As you say the documentation if pretty fragmented. Just to add - prior to calling FB.logout I had to call FB.init as documented on the FB Javascript API page. Thanks for your help.Marylnmarylou
Just up update, this solution doesn't work in Chrome (5.0.375 at least) on a mac. The user isn't logged out of FB, works fine in Safari and Firefox though..Marylnmarylou
My logout page had to do some FB login checking. The FB.logout() onclick did not complete fast enough before the redirection. I had to delay the redirection to ensure FB.logout() completes successfully first.Psoriasis
C
19

This solution no longer works with FaceBook's current API (seems it was unintended to begin with)

http://m.facebook.com/logout.php?confirm=1&next=http://yoursitename.com;

Try to give this link on you signout link or button where "yoursitename.com" is where u want to redirect back after signout may be ur home page.

It works..

Chiastolite answered 28/7, 2010 at 6:14 Comment(4)
Sumit, thank-you. That works perfectly. I'm not entirely sure why the mobile version of facebook lets you log-out like that while the other doesn't but I don't care. It's better than the jscript hack I'm using at the moment, and easier than the main facebook logout + session key described around here as well.Marylnmarylou
But what if the user doesn't want their facebook session to be destroyed? What if they were already logged into face book, then log into your site, then log out of your site, then go back to facebook and don't understand why they've just been logged out? This doesn't seem like the perfect solution...Very
When this question was asked a few months ago, single sign out was a requirement of the facebook terms and conditions. I'm not sure if it still is, and I don't disagree with you, but at the time it was required.Marylnmarylou
see my solution below for something that works as of now, and is documented by facebookWorship
W
19

This works as of now - and is documented on facebook's site @ http://developers.facebook.com/docs/authentication/. Not sure how recently it was added to the documentation, pretty sure it wasn't there when I checked Feb-2012

You can programmatically log the user our of Facebook by redirecting the user to

https://www.facebook.com/logout.php?next=YOUR_REDIRECT_URL&access_token=USER_ACCESS_TOKEN

Worship answered 21/3, 2012 at 6:9 Comment(2)
Can't get this to work. Using server side connect method. Also, if once the documentation link pointed to this method, it doesn't anymore.Memnon
@DanielGerson when you use ssc you will get the access token as response from facebook. It will work when you send the access token to the client, so that the client can used it in the logout link.Coadjutant
I
11

I can programmatically log user out Facebook by redirecting user to

https://www.facebook.com/logout.php?next=YOUR_REDIRECT_URL&access_token=USER_ACCESS_TOKEN

The URL supplied in the next parameter must be a URL with the same base domain as your application as defined in your app's settings.

More details: https://developers.facebook.com/docs/authentication

Imco answered 9/10, 2012 at 4:27 Comment(0)
S
6

You can do this with the access_token:

$access_array = split("\|", $access_token);

$session_key = $access_array[1];

You can use that $session key in the PHP SDK to generate a functional logout URL.

$logoutUrl = $facebook->getLogoutUrl(array('next' => $logoutUrl, 'session_key' => $session_key));

This ends the browser's facebook session.

Supremacist answered 29/5, 2010 at 21:11 Comment(6)
you don't need to work with token manually due to $facebook already has it.Cyprinid
Could you explain what you mean in more detail? This method is the only one I've found to log the user out and end the FB session without using javascriptSupremacist
Bob, here's the structure of the logout URL: facebook.com/…> Where session_key is the middle part of the access token as I described above.Supremacist
As @Amir pointed out, the session key is the same as the "code" you received from Facebook during the inintial OAuth authorization. Better to use that one than to try to parse the access token (since the format of the token could change).Platino
Using the same method Zach used. Here is how to do a complete logout in RoR + Devise + Omniauth github.com/intridea/omniauth/wiki/…Fiesta
great this is working, but little corrections here, split is deprecated so use explode function and also first parameter should be "|" not "\|" :)Brief
M
2

With PHP I'm doing:

<a href="?action=logout">logout.</a>

if(isset($_GET['action']) && $_GET['action'] === 'logout'){
    $facebook->destroySession();
    header(WHERE YOU WANT TO REDIRECT TO);
    exit();
}

Works and is nice and easy am just trying to find a logout button graphic now!

Monserratemonsieur answered 14/3, 2014 at 10:53 Comment(0)
I
2

Here's an alternative to the accepted answer that works in the current (2.12) version of the API.

<a href="#" onclick="logoutFromFacebookAndRedirect('/logout')">Logout</a>

<script>
    FB.init({
        appId: '{your-app-id}',
        cookie: true,
        xfbml: true,
        version: 'v2.12'
    });

    function logoutFromFacebookAndRedirect(redirectUrl) {
        FB.getLoginStatus(function (response) {
            if (response.status == 'connected')
                FB.logout(function (response) {
                    window.location.href = redirectUrl;
                });
            else
                window.location.href = redirectUrl;
        });
    }
</script>
Ironclad answered 22/3, 2018 at 19:33 Comment(0)
W
1

the mobile solution suggested by Sumit works perfectly for AS3 Air:

html.location = "http://m.facebook.com/logout.php?confirm=1&next=http://yoursitename.com"

Whirly answered 13/9, 2010 at 21:25 Comment(0)
C
1

A note for Christoph's answer: Facebook Oauth Logout The logout function requires a callback function to be specified and will fail without it, at least on Firefox. Chrome works without the callback.

FB.logout(function(response) {});
Confabulation answered 26/11, 2010 at 19:45 Comment(0)
B
1

For Python developers that want to log user out straight from the backend

At the moment I'm writing this, the trick with m.facebook.com no longer works (at least for me) and user is redirected to the mobile FB login page which obviously is not good for UX.

Fortunately, FB PHP SDK has a semi-documented solution (in case the link doesn't lead to getLogoutUrl() function, just search look for it on that page). This is also mentioned in at least one other on StackOverflow: Facebook php SDK getLogoutUrl() problem.

BTW I've just noticed that Zach Greenberg got it right in this question, but I'm adding my answer as a summary for Python developers.

Boiled answered 15/5, 2011 at 13:43 Comment(0)
M
1

@Christoph: just adding someting . i dont think so this is a correct way.to logout at both places at the same time.(<a href="/logout" onclick="FB.logout();">Logout</a>).

Just add id to the anchor tag . <a id='fbLogOut' href="/logout" onclick="FB.logout();">Logout</a>



$(document).ready(function(){

$('#fbLogOut').click(function(e){ 
     e.preventDefault();
      FB.logout(function(response) {
            // user is now logged out
            var url = $(this).attr('href');
            window.location= url;


        });
});});
Mezuzah answered 8/12, 2013 at 21:16 Comment(0)
V
1

Update: This solution works and just a call to 'FB.logout()' doesn't work because browser wants a user interaction to actually call this function, so that it knows - it is a user not a script.

<a href="#" onclick="FB.logout();">Logout</a> 
Verdugo answered 28/2, 2014 at 11:17 Comment(0)
R
-7

it's simple just type : $facebook->setSession(null); for logout

Rolfrolfe answered 17/7, 2010 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.