que1326
que1326

Reputation: 2325

Manage permissions with Facebook JS SDK

I have a list of fb permissions that I want to select / deselect and then I want to submit the changes to facebook with this logic with just one user action ( one click ):

1) login to FB;

2) delete ( revoke ) all permissions;

3) request the new ( selected ) permissions.

<button onclick="changePermissions()">Change permissions</button>

function changePermissions() {
        FB.login((response) => {

            FB.api('/me/permissions', 'DELETE', () => {

                FB.login(() => {}, {
                    scope: 'email,publish_pages', // the new selected permissions
                    auth_type: 'rerequest'
                });
            });
        });
}

Step 1) and 2) works as expected, but step 3) fails. ( the new login pop-up with the new permissions is blocked by the browser ). Facebook js sdk says that "FB.logion() must be called from button event handler".

How can I achieve this scenario with just one user action ( 1 click ). I don't want to force the user to interact multiple times with the UI in order to change his permissions ?

Upvotes: 0

Views: 581

Answers (1)

andyrandy
andyrandy

Reputation: 73984

If the user authorized your App already, you can just delete the permissins BEFORE FB.login. FB.getLoginStatus re-authorizes the user anyway. So the first FB.login is not needed, but the API call for deleting permissions is ansynchronous, that is why browser block the second FB.login call. Just delete the permissions right after FB.getLoginStatus and only do FB.login in the changePermissions function.

Example with FB.getLoginStatus: https://www.devils-heaven.com/facebook-javascript-sdk-login/

Upvotes: 1

Related Questions