0

I'm using a Facebook SDK for JavaScript login for my facebook app for my webpage and I have a main login page. How can i redirect users after they've logged in. If they didn't log in they can't continue to the next page using this.

https://developers.facebook.com/docs/facebook-login/login-flow-for-web

Below is the script from the website!. In this script ive added a redirect into it but that does not seem to work.

<script>
  window.fbAsyncInit = function() {
  FB.init({
    appId      : 'APP_ID',
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

  // Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
  // for any authentication related change, such as login, logout or session refresh. This means that
  // whenever someone who was previously logged out tries to log in again, the correct case below 
  // will be handled. 
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
       window.location = "http://www.google.com/" <!-- This is what i tried -->
      // The response object is returned with a status field that lets the app know the current
      // login status of the person. In this case, we're handling the situation where they 
      // have logged in to the app.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // In this case, the person is logged into Facebook, but not into the app, so we call
      // FB.login() to prompt them to do so. 
      // In real-life usage, you wouldn't want to immediately prompt someone to login 
      // like this, for two reasons:
      // (1) JavaScript created popup windows are blocked by most browsers unless they 
      // result from direct interaction from people using the app (such as a mouse click)
      // (2) it is a bad experience to be continually prompted to login upon page load.
      FB.login();
    } else {
      // In this case, the person is not logged into Facebook, so we call the login() 
      // function to prompt them to do so. Note that at this stage there is no indication
      // of whether they are logged into the app. If they aren't then they'll see the Login
      // dialog right after they log in to Facebook. 
      // The same caveats as above apply to the FB.login() call here.
      FB.login();
    }
  });
  };

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

  // Here we run a very simple test of the Graph API after login is successful. 
  // This testAPI() function is only called in those cases. 
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Good to see you, ' + response.name + '.');
    });
  }
</script>

- Under if (response.status === 'connected') { ive added the windows redirect but that dont seem to work at all!.Ive tried adding this

FB.login(function(response) {
    if (response.authResponse) {
        // The person logged into your app
        window.location = "http://www.google.com/"
    } else {
        // The person cancelled the login dialog
        // Nothing happens
    }
});

This does not work neither when i've added this This is what i've later added to the login flow

$params = array(
  'scope' => 'read_stream, friends_likes, user_website, user_checkins',
  'redirect_uri' => 'https://www.mywebsite.com/blablabla'
);

$loginUrl = $facebook->getLoginUrl($params);

Ive added a couple scopes to it and put it with the login flow. It still don't redirect users to another page after login, nor redirect users if the login was unsuccessful

BrainDamage
  • 34
  • 1
  • 9

1 Answers1

0

Maybe this has something to do with the security settings in the Browser (CORS), see https://developer.mozilla.org/en-US/docs/Web/API/Window.location#Example_.231.3A_Navigate_to_a_new_page

There are some answers here: Redirect from Facebook canvas page to website

Additionally, you should be able to set the redirect_uri as described here: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/

Community
  • 1
  • 1
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • When people login to there facebook account registered to my facebook app it stills stay on the login page. Nothing changed. Ive tried everything and its still not working – BrainDamage Jan 27 '14 at 14:43
  • Just set the redirect_uri before triggering the login dialog as described here: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/#parameters – Tobi Jan 27 '14 at 21:12