Link to home
Start Free TrialLog in
Avatar of tangteng78
tangteng78Flag for Malaysia

asked on

How to login using FB api and redirect to next page?

Hi,
I'm able to login to FB using the FB api, but to redirect to the next page (logged in page), i need to click refresh.

Is there anything that i missed out? Any help is greatly appreciated.
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>muncch</title>

      <script src="http://connect.facebook.net/en_US/all.js">
      </script>

      <script>
          function bodyload()
          { 
          FB.init({
              appId: '123455667', cookie: true,
              status: true, xfbml: true
          });

          FB.api('/me', function (user) {
              if (user.id != null) {
                  var image = document.getElementById('image');
                  image.src = 'http://graph.facebook.com/' + user.id + '/picture';
                  var name = document.getElementById('name');
                  name.innerHTML = user.name

                  var txtUserID = document.getElementById('txtUserID');
                  txtUserID.value = user.id;

                  //With user login, now redirect user to new page
                  window.location = "http://www.xxx.com/icanadd/json.aspx"
                  //alert('im logged on');
              }
              else {
                  alert('im nothing');
              }
          });

         

      }
      </script>


  </head>
 
  <body onload="bodyload();">

      <form id="form1" runat="server">

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

        <a href="https://www.facebook.com/dialog/oauth?client_id=123456&redirect_uri=http://www.xxx.com/icanadd/json.aspx">click to login</a>
        </form>

    </body>

</html>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

fbEnsureInit(function() {
    location.href = "http://www.xxx.com/icanadd/json.aspx";
});

Open in new window

Avatar of tangteng78

ASKER

Okay, now it's able to redirect to json.aspx, great.

 But the same issue exist on this page(json.aspx), even though i'm redirected to the page, i still need to click refresh for me to be able to access the fb logged in cookie.
Any help is appreciated guys. Thanks.
What about :

		FB.Event.subscribe("auth.logout", function(response) {
			alert("You loggout");
		});

Open in new window


or :

		FB.Event.subscribe("auth.login", function(response) {
			t = setInterval(FB.getLoginStatus(function(response) { if(response.session && response.status != "connected") { clearInterval(t) } }), 1500);
			alert("You're logged");
		});

Open in new window

Doesn't work.

After logged in and redirect to the json.aspx page, i still need to refresh (hit F5) before i'm able to get the FB cookie so that i can extract the Graph API.

Is there an example out there that shows the whole nine yards from authentication to logged in page??

The first one take its time but it work
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial