Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect after clicking on the Facebook login button, even if already logged in Facebook

I have the standard Facebook login button on my homepage and I don't want people to automatically log into my site with their Facebook account, only if the user clicks the login button.

If the user is not logged in Facebook, a popup will appear asking him his credentials and he will be redirected to loggedin.html after that.

<div id="fb-root"></div>
<fb:login-button perms="email"></fb:login-button>

<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId  : 'xxxxxxxxxxxx',
            status : true,
            cookie : true,
            xfbml  : true
        });

        FB.Event.subscribe('auth.login', function() {
            window.location = "loggedin.html";
        });
    };

    (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());
</script>

If the user is already logged in Facebook, when he clicks the button the popup appear and disappear right away, I am OK with that. But the user is not redirected to loggedin.html. How can I do that ?

like image 550
Quentin Avatar asked Apr 29 '11 18:04

Quentin


People also ask

What is the login button on Facebook?

The Login button is a simple way to trigger the Facebook Login process on your website or web app. If someone hasn't logged into your app yet, they'll see this button, and clicking it will open a Login dialog, starting the login flow. People who have already logged in won't see any button, or you can also choose to show a logout button to them.

How to integrate Facebook login to your app?

After clicking on the Create App ID the facebook will redirect you to the Select a Scenario screen here choose Integrate Facebook Login option and click on Confirm button. Now you are in the App Dashboard which will looks like this

What does the continue with Facebook button do?

The Continue with Facebook button replaces earlier versions of the Login button. For more information, see Migration. The Continue as {Name} button has the text "Continue as {persons' name}" and optionally includes the person's Facebook profile picture if they're logged in to Facebook in the same browser.

Why can't I load a URL on my Facebook app?

Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings. These errors are stricly related to facebook application settings.


1 Answers

If you use the PHP SDK, you can use the following code:

require_once 'path/to/facebook.php';
define('A_ID', '*YOUR APP ID*');
define('A_SECRET', 'YOUR APP SECRET');

$facebook = new Facebook(array('appId' => A_ID, 'secret' => A_SECRET, 'cookie' => true));
$userId = $facebook->getUser();

if (!$userId):
  ?>
    <!-- HTML to show if the $userId isn't available (user isn't logged in) -->
  <?php
else:
  ?>
    <!-- HTML to show if the $userId is available (user is logged in)-->
  <?php
endif;

Ideally you should use the PHP SDK as it gives you more control over the data and the way it is shown, than using the JS SDK to insert data.

Facebook have a reference for the PHP SDK here: developers.facebook.com/docs/reference/php/

The JavaScript SDK reference is here: developers.facebook.com/docs/reference/javascript/

like image 153
Andrew Willis Avatar answered Sep 21 '22 06:09

Andrew Willis