0

What would I need to do to allow asynchronous login/authentication (permissions) between an external site for mobiles and a facebook tab app.

I essentially need one system that does both desktop/mobile.

Example

Case 1 (Mobile/external access) - Logs in using Javascript using login and works fine app works perfectly. - This works the way it's supposed to which is great!

case 2(Facebook user - tab app) - Logs in and gains permissions through PHP SDK onviously the user is already logged in. My problem is javascript knows the user is logged in but still asks the user to login for the permissions they've already given.

TLDR: Why won't Javascript pickup the Auth cookie from the PHP SDK and recognise the user has the correct permissions from PHP? This login flow is fine when the user has logged in via mobile or externally to the app. I've tried them seperately and they work fine just won't work together.

I've seen it working the other way around but I want JS only or PHP>JS NOT JS>PHP.

*Edit

window.fbAsyncInit = function() 
{
    FB.init({
    appId      : 'APP_ID_HERE', // App ID 
    channelUrl : 'channel.html', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true,  // parse XFBML
    oauth      : true // enable OAuth 2.0
});

my event handlers for login/logout and likes goes here.

    (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 fblogin() 
{
    FB.login(function(response) 
    {
    },
    { scope: 'email, user_likes' }
    );
}

function createRequestObject() {
    var obj;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
    {
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else 
    {
        obj = new XMLHttpRequest();
    }
    return obj;
}

Login for mobile/desktop works great it's picking up the scope but when I use the following code to setup the tab app, the permissions won't transfer to the javascript session.

PHP Code (which works):

<?php
require_once 'src/facebook.php'; // get facebook sdk
$facebook = new Facebook(array(
    'appId' => 'APP ID HERE',
    'secret' => 'APP SECRET HERE'
));

$user = $facebook->getUser();
$location = "". $facebook->getLoginUrl(array('scope' => 'email, user_likes'));

// check if we have valid user
if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.  
        $permissions = $facebook->api('/me/permissions', 'get', array('access_token'=>$access_token));
    } catch (FacebookApiException $e) {
        $fb_user_id = NULL;
        // seems we don't have enough permissions
        // we use javascript to redirect user instead of header() due to Facebook bug
        print '<script language="javascript" type="text/javascript"> top.location.href="' . $location .'"; </script>';

        // kill the code so nothing else will happen before user gives us permissions
        echo $e->getMessage();
        die();
     }
    } else {
        // seems our user hasn't logged in, redirect user to a FB login page
        print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';
    // kill the code so nothing else will happen before user gives us permissions
    die();
}

// at this point we have an logged in user who has given permissions to our APP
// Facebook opens canvas page (which is the mobile/external page) but doesn't transfer permissions.

PHP works great it transfers the user to the mobile (canvas) page with login/like button. How do I get PHP to pass the perms to my canvas where my mobile/desktop site is?

*EDIT 2: HTML/PHP:

<div class="container">
<div id="fb-root"></div>
<div id="facebook-div">
    <fb:login-button autologoutlink="true" scope="email,user_likes" size="large"></fb:login-button>
    <div id="facebook-right" class="fb-like" data-href="https://www.facebook.com/154456204613952" data-width="90" data-layout="button_count" data-show-faces="false" data-send="false"></div>
</div>
//Rest of the code here
4
  • You’ll have to get a little more specific on what your code looks like, especially the JS part within the page tab app. Commented Oct 11, 2013 at 17:19
  • Thanks CBroe - I thought enabling the cookie within JavaScript would work or can this system only work where you would login via javascript first? Commented Oct 13, 2013 at 22:16
  • No, it should work both ways. But I still don’t see where you actually call your JS login function, resp. under what conditions you do or don’t call it. Commented Oct 14, 2013 at 6:03
  • Top of my container (for mobile/desktop) has login/like button. The tab app recognises login but still asks for perms (login instead of logout). The only way it should say login is if the user is on a mobile or desktop (away from facebook). Commented Oct 14, 2013 at 10:20

1 Answer 1

0

Managed to resolve it thanks for your help! After the PHP instantiated I didn't include the Canvas php file after the user was authenticated... I've been pulling my hair out for months trying to resolve this! This authentication gets passed to Javascript which deals with everything!

This means that both PHP and Javascript calls are able to work within the same application.

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.