简体   繁体   中英

Facebook oauth javascript redirect resulting in endless loop

I want to redirect users to the oauth page if they have removed any of the permissions my app requires.

For some reason the below code results in an endless loop when I try to redirect from the FB.api callback function. Any ideas how I can fix this?

var perms           = ['publish_actions', 'email', 'user_birthday', 'user_location'],
    permsString     = perms.join(','),
    permissionsUrl  = 'https://www.facebook.com/dialog/oauth';
    permissionsUrl  += '?client_id=' + config.facebook.appId;
    permissionsUrl  += '&redirect_uri=' + encodeURI(canvasUrl);
    permissionsUrl  += '&scope=' + permsString;

    FB.getLoginStatus(function (response) {

        if (response.status === 'connected') {

            FB.api('/me/permissions', function(response) {

                // using underscore here...
                var keys = _.keys(response.data[0]),
                    diff = _.difference(perms, keys);

                // send the user through the auth again if they've removed any of the perms we need
                if (diff.length) {

                    window.location.href = permissionsUrl; // results in an endless redirect loop
                    // window.location.href = 'http://randomwebsite.com'; // does redirect successfully!!!!
                }
            });
        }

    }, true);

It's been a while since I did this but from memory I solved it with something like this:

var redirectMe = function (link) {
  window.location.href = link;
};

FB.getLoginStatus(function (response) {
    if (response.status === 'connected') {
        FB.api('/me/permissions', function(response) {
            if (true) {
                redirectMe('http://www.browsehappy.com');
            }
        });
    }
}, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM