javascript - Facebook 登录 Javascript SDK 自动登录(如果设置了 cookie)

标签 javascript facebook facebook-javascript-sdk

我正在尝试设置我的网站,以便当您进入登录页面时,它会检查是否设置了 cookie,如果设置了,则登录应该是自动的 - 即,当此页面加载时,它会重定向到“Facebook.ashx” "它创建一个 cookie 来记住访问 token 。

但是,如果未设置 cookie,则用户必须单击“使用 Facebook 登录”按钮才能继续登录,然后继续像以前一样重定向到“Facebook.ashx”。

这是我的代码 - 目前,一旦用户接受应用程序并继续“Facebook.ashx”,它总是会登录

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

        // Additional initialization code here

        FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.status === 'connected') {
                // the user is logged in and has authenticated your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                // TODO: Handle the access token
                // Do a post to the server to finish the logon
                // This is a form post since we don't want to use AJAX
                var form = document.createElement("form");
                var redirect = "FacebookLogin.ashx?redirect=" + document.getElementById('redirect').innerHTML;
                form.setAttribute("method", 'post');
                form.setAttribute("action", redirect);

                var field = document.createElement("input");
                field.setAttribute("type", "hidden");
                field.setAttribute("name", 'accessToken');
                field.setAttribute("value", accessToken);
                form.appendChild(field);

                document.body.appendChild(form);
                form.submit();

            } else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                // but has not authenticated your app
            } else {
                // the user isn't logged in to Facebook.
            }
        });
    };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));
</script>

<div class="fb-login-button" scope="email, publish_stream">Login with Facebook</div>

感谢您提前提供帮助

最佳答案

只要用户登录,

auth.authResponseChange 就会在每个页面加载时触发。

您想要做的是将“已知状态”从服务器传递到客户端,然后在 FB.getLoginStatus 中进行比较。如果状态发生变化(例如 userID),则这构成真正的登录,您将重定向到 ashx 页面以设置新的 cookie。

举个简单的例子:

  • 第一步 - 加载页面,刷新空状态 (userID = 0)
  • 第二步 - 执行 FB.getLoginStatus,将 authResponse.userID 与 userID 进行比较
  • 第三步 - 由于 authResponse.userID <> 0,重定向到设置 cookie 的页面
  • 第四步 - 加载页面,刷新状态(userID = authResponse.userID 中的 userID)
  • 第五步 - 执行 FB.getLoginStatus,将 authResponse.userID 与 userID 进行比较,因为它们匹配,但不执行任何操作。

关于javascript - Facebook 登录 Javascript SDK 自动登录(如果设置了 cookie),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12271325/

相关文章:

javascript - 循环数组以修改值(JavaScript 到 PHP 并返回到 JavaScript)

javascript - 如何使用 jQuery UI 拖动元素时触发 droppable 事件?

facebook - 如何使用 FB Graph API 从事件中获取出席人数?

ajax - 从 Facebook 返回的 X-FB-DEBUG header 是什么?

Ajax帖子找不到 Action Controller

javascript - 聚焦 DIV 上的单击操作

javascript - 在 HTML5 Canvas 中定位文本

css - Facebook Like 按钮发布到 Facebook Box 显示不正确?

ios - 在 Facebook 应用程序中打开帖子的 URL Scheme

iOS facebook - 重新请求丢失的权限