开发者

How can I detect that a user signed into Facebook?

开发者 https://www.devze.com 2023-02-23 15:08 出处:网络
Here is my scenario. When a user opens a browser and goes to facebook.com to sign himself in, I want to be able to detect that and init m开发者_如何学运维y Facebook application sign-in process.

Here is my scenario. When a user opens a browser and goes to facebook.com to sign himself in, I want to be able to detect that and init m开发者_如何学运维y Facebook application sign-in process.

Is that possible? I'm asking because I noticed that signing into Facebook itself doesn't get my app signed in automatically, which is not good.

I'd prefer JavaScript client code.


UPDATE: Here you have a FULLY WORKING example. The only thing you will need to do is to replace the APP_ID for whatever ID you get when sign your app at: http://www.facebook.com/developers/ and the port you may use for the localhost test. This detects when the user logs in even in another browser window. It is obviously extremely basic but proves the point that is feasible with the tools from the graph API.


You can add this code to detect when the user logs in. It is the same oauth log in whether the user clicks on a <fb:login-button> or logs directly onto facebook.com:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Hello App Engine</title>
        <SCRIPT type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></SCRIPT>
  </head>

  <body>
    <h1>Hello!</h1>

<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
    appId  : APP_ID,
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
});

/*
 * Authentication functions
 */
FB.Event.subscribe('auth.login', function (response) {
    welcomeMsg(response);
});

FB.Event.subscribe('auth.logout', function (response) {
    alert ("Good bye!");
});

if (FB.getSession() != null) {
    FB.api('/me', function(response){
        console.log(response.name);
        welcomeMsg(response);

    })
} else {
    window.setTimeout(function(){window.location.href="http://localhost:8888"},5000);
}


function welcomeMsg(userData) {
    console.log ("Welcome " + userData.name);
}
    </script>
  </body>
</html>


You can subscribe to the auth.login event. http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/


There are many Facebook images that you can see only if you're logged into Facebook. You can make the user attempt to load one of these images, and then detect if it loaded or not. It's a total trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号