2012-06-08 65 views
1

我的问题是:
如果我已经登录Facebook,那么它会自动重定向页面并登录到我的网站。如果我已经登录Facebook,请避免自动登录网站

我需要的是即使我登录Facebook,它应该留在页面上,当我点击“Facebook登录”按钮后,它会登录到我的网站。

<script> 
    // 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)); 

    // Init the SDK upon load 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId: 'XXXXXXXXXXX', // App ID 
      channelUrl: '//' + window.location.hostname + '/channel', // Path to your  Channel File 
      status: true, // check login status 
      cookie: true, // enable cookies to allow the server to access the session 
      xfbml: true // parse XFBML 
     }); 

     FB.Event.subscribe('auth.login', function(response) { 
      if (response.status === 'connected') { 
       FB.api('/me', function(me) { 
        if (me.name) { 
         // the user is logged in and connected to 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; 
         done1(uid, accessToken, me.email, me.name); 
        } 
       }) 
      } 

     }); 

     FB.Event.subscribe('auth.logout', function(response) { 
     }); 


     // listen for and handle auth.statusChange events 
     FB.Event.subscribe('auth.statusChange', function(response) { 
      if (response.authResponse) { 
       // user has auth'd your app and is logged into Facebook 
       FB.api('/me', function(me) { 
        if (me.name) { 
         document.getElementById("authdisplayname").innerHTML = me.name; 
         if (response.status === 'connected') { 
          var uid = response.authResponse.userID; 
          var accessToken = response.authResponse.accessToken; 
          done1(uid, accessToken, me.email, me.name); 
         } 
        } 
       }) 
       document.getElementById('auth-loggedout').style.display = 'none'; 
       document.getElementById('auth-loggedin').style.display = 'block'; 
      } else { 
       // user has not auth'd your app, or is not logged into Facebook 
       document.getElementById('auth-loggedout').style.display = 'block'; 
       document.getElementById('auth-loggedin').style.display = 'none'; 
      } 
     }); 
     $("#authlogoutlink").click(function() { FB.logout(function() { window.location.reload(); }); }); 
    } 

    function done1(a, b, c, d) { 
     document.getElementById("<%= uid.ClientID %>").value = a; 
     document.getElementById("<%= token.ClientID %>").value = b; 
     window.location = "http://localhost:59019/fgfgf_2008/new_login.aspx?token=" + b + "&email=" + c + "&name=" + d; 
    } 
</script> 

HTML代码:

<div id="fb-root"> 
</div> 

<div> 
    <div id="auth-status"> 
     <div id="auth-loggedout"> 
      <div class="fb-login-button" onlogin="done();" autologoutlink="true" scope="email,user_checkins"> 
       Log in with Facebook</div> 
     </div> 
     <div id="auth-loggedin" style="display: none"> 
      Hi, <span id="authdisplayname" runat="server"></span>(<a href="#" id="authlogoutlink">logout</a>) 
     </div> 
    </div> 
</div> 
+0

后,它执行'如果(response.status === '连接')'然后,如果我在Facebook上已经登录,它只是调用'DONE1()'功能并重定向并登录到该站点。 –

回答

2

我不是Facebook登录问题的高手,但在阅读您的问题后,它看起来像,如果Facebook的用户在其Facebook帐户登录,那么它也会自动登录到您的应用程序中。但是,你希望它发生在用户点击登录按钮的时候,正确的。

你可以做的是,你可以做一个“FacebookLogin()”函数,你可以在点击事件中调用该函数。

喜欢的东西:

在HTML按钮

<div class="fb-login-button" onclick="FacebookLogin()" onlogin="done();" autologoutlink="true" scope="email,user_checkins"> 
       Log in with Facebook</div> 

我不是为了这个非常肯定你的情况,但它的工作对我来说,尝试一次,删除所有Ur FB.Event.subscribe('auth.login', function(response)功能,并使用FB.login()函数,然后将用户信息重定向到您想要的任何页面。在这里,我将它重定向到一个Login.aspx页面。

在javascript函数

function FacebookLogin() { 
    FB.login(function(response) { 
     if (response.authResponse) { 
      window.location = "/Login.aspx?NewFacebookConnect=true&accessToken=" + response.authResponse.accessToken + "&userID=" + response.authResponse.userID; 
     } 
    }); 
} 
+0

太棒了......我在尝试自己的答案之前,我看了你的答案..但它仍然适用于我,所以我接受你的答案......谢谢。 –

+0

我的快乐哥们。 :) – Shekhar