FB JavaScript iframe application is not auto redirecting to FB login page if no session available

∥☆過路亽.° 提交于 2019-12-24 11:01:54

问题


I am developing facebook iframe app with JavaScript.. the problem is when the user hits the app URL https://apps.facebook.com/projectanida/ I want users to see the application main page if they are already logged into fb and if there is no user session available then the user should be automatically directed to FB page asking for credentials to log into Facebook to use my app.

Here is the code :

var href = "https://projectafacebook.appspot.com/fb/";
var appliId = "165346656890746";

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

    });

  FB.Event.subscribe('auth.sessionChange', function (response) {
                               if (response.session) {
                                   //user is logged in!
                                   alert("//user is logged in!");
                                 //  window.location = document.URL;
                               } else 
                               {
                                   alert("//user is not logged in!");
                            windows.top.location.href = 'https://graph.facebook.com/oauth/authorize?client_id=' + appliId + '&redirect_uri=' + href + '&display=page';

                                   }
                            }); 
};
 FB.getLoginStatus(function(response) {
            if (response.session) {
                //alert("session true");
                //$("#fb-root").show();
            } else {
                top.location.href = 'https://graph.facebook.com/oauth/authorize?client_id=' + appliId + '&redirect_uri=' + href + '&display=page';
            }
        });

the if part works fine but when the else part come it shows fb login page but below the blue bar of fb page it shows my application and is also accessible even if no session is available... it works fine in chrome and IE8 some times but never in opera n firefox.

and if we login on that page it still direct to my app page in fb but the canvas size is fixed and my app got cut where as if i login first and then hit my app URL then the applications works fine and canvas size is also fine. please help!


回答1:


You shouldn't be using auth.sessionChange anymore, try with auth.statusChange or auth.authResponseChange.

Also is there any reason you're redirecting to a specific URL when you find there is no session instead of using FB.login()?

Try this:

FB.Event.subscribe('auth.statusChange', handleUserStateChange);
FB.getLoginStatus(handleUserStateChange);

function handleUserStateChange(response) {
    // User is not logged into Facebook OR hasn't authenticated app
    if(!response.authResponse) {
        // http://developers.facebook.com/docs/reference/javascript/FB.login
        FB.login(function(response) {
            if(response.authResponse) {
                window.location.reload();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/8207753/fb-javascript-iframe-application-is-not-auto-redirecting-to-fb-login-page-if-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!