开发者

How often is a Facebook Connect session refreshed?

I have set up Facebook Connect on my site, and the login is working fine. I have set up a listener to detect a login state change, as follows:

FB.Event.subscribe('auth.sessionChange', function() {
        window.location.reload();
    });

All good. Except that leaving a browser window open causes the page to reload after perhaps 20mins. So the auth.sessionChange is firing at some interval. Which can caused havoc, particularly if the last page was a POST form submission.

In the FB docs it says "Sessions are refreshed over time as long as the user is active with your app." How often is this session refreshed?

And how can I protect the page from reloading if it doesn't need to? A conditional maybe within that function??

EDIT - adding information for clarification:

I thought I would add some info to give those offering the advice some more to go on (thanks so far BTW):

the reason I have the listener triggering a reload is because I wanted users to be logged in to the site every time they visit - if they already had a session in FB. I was detecting an act开发者_开发技巧ive session with the JS SDK, which I know could do log in on its own, but I needed to trigger a page reload after the JS had done the detection, in order to set a PHP session for the site - required step. I couldn't do the PHP login without first letting the JS detect the FB session in the browser.


Usually you can see the length of the validity of the session just by looking at the session itself. However in many cases, I suggest requesting the offline_access permission to ensure the access_token remains valid as long as the user doesn't change their password and doesn't remove your application.


Andy, if you are only listening for login state then subscribe to auth.login and auth.logout instead of auth.sessionChange.

If you need an active session for a user to take an action on your site, use FB.getLoginStatus() or FB.getSession() to check the session object.

http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

http://developers.facebook.com/docs/reference/javascript/FB.getSession/


It really depends on your App work-flow and the below is just a general approach.

You can have a flag that presents in all your pages (maybe in your main layout if you are using a template system) with a setter function to be executed once you don't need a page reload to happen. Something like:

var do_reload = true; // By default, we always allow a reload

FB.Event.subscribe('auth.sessionChange', function() {
    if( do_reload ) {
        window.location.reload();
    }
});

// Call this whenever you don't want 'auth.sessionChange' to reload the page
function turnoff_reload() {
    do_reload = false;
}

// Call this to return to default behavior
function turnon_reload() {
    do_reload = true;
}

Now if you are "redirecting" the user (e.g.: after a form post) to a page that doesn't require a reload then you need to set the flag in the first line to false.

Another scenario if you have an ajax loaded content based on the user interaction, if the page reloads by itself that content will be lost and the user needs to interact again to view that content, what can be done here is setting the flag to false after the Ajax call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜