0

I am making facebook login as shown below. But events are not trigger ( FB.Event.subscribe(..), GetFBLoginStatus()). I run it at localhost and I specify it in my application,but for this purpose it is not necessary. It is looking like javascript not loaded,but facebookbutton rendered.What must I change?Pop-up window asks permissions when i click 'login' but nothing more.

     <html>
<head>
  <title>My Facebook Login Page</title>
</head>
<body>

  <div id="fb-root"></div>
  <script type ="text/javascript">
      window.fbAsyncInit = function () {
          FB.init({
              appId: 'some id', // App ID
              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) { alert('You logged in'); });
      };
      // 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));

      function GetFBLoginStatus() {
          FB.getLoginStatus(function (response) {
              console.log(response);
              if (response.status === 'connected') {
                  var accessToken = response.authResponse.accessToken;
                  alert(accessToken);
              } else if (response.status === 'not_authorized') {
                  //login function
                  alert('login first');
              } else {
                  //login function
                  alert('login first');
              }
          }, true);
      }
  </script>
    <div class="fb-login-button"  scope="email,user_checkins,read_mailbox">Login with Facebook</div>
      //this function is not work -> javascript not loaded?
    <button onclick = "GetFBLoginStatus();">GetStatus</button>
</body>

0

1 Answer 1

1

Maybe the problem is the localhost is not accepting coockies. This happens a lot. It's necessary to have cookies enabled, because that's the way FB JS SDK passes the user login information to your page. I usually edit my system/hosts to access localhost via www.localhost.com. With that the browsers don't detect the localhost and writes all the necessary cookies.

Also, it is better to define your GetFBLoginStatus() function inside window.fbAsyncInit, because it can be called before the FB.init and will crashes because the FB object (used inside your function: FB.getLoginStatus) is not created yet.

I heard some people say that sometimes the auth.login event does not triggers when the user already authorized your app. That's because this event only triggers when the status CHANGES to connect. If the user has authorized in the past, nothing has changed. There are other events, like auth.authResponseChange and auth.statusChange. Maybe they work better for your needs.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your useful remarks,Victor.Also I recommend for developers this facebookhelper.codeplex.com
for using this package in mvc add reference webmatrix.data && webmatrix.webdata and set in properties copy lockally 'true'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.