如果设置了cookie,则Facebook登录Javascript SDK自动登录

Facebook Login Javascript SDK autologin if a cookie is set

本文关键字:登录 Javascript SDK Facebook 设置 cookie 如果      更新时间:2023-09-26

我正在尝试设置我的网站,这样当你进入登录页面时,它会检查是否设置了cookie,如果设置了,那么登录应该是自动的——也就是说,当这个页面加载时,它重定向到"Facebook.ashx",它会创建一个cookie来记住访问令牌。

但是,如果没有设置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,因为它们匹配,什么都不做

我解决了这个问题-简单修复

如果你将状态更改为false而不是true,那么它会等待用户点击登录并检查cookie是否存在——只需一个简单的If语句即可将状态更改成true或false,如下所示:

if (document.cookie.indexOf("fb_token") > 0) //user has already logged in with facebook - process should be automatic
        var fb_status = true;
    else
        var fb_status = false;
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'My App ID', // App ID
            status: fb_status, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });