2
I have a usability problem when requesting extra permissions on Facebook.
According to Facebook documentation the function FB.login
should only be called after a click as most browsers block open pop-ups by any other means.
The problem occurs when I’m using the function FB.login
in the middle of my application in order to request extra permissions to post an image (according to Facebook’s best practices I leave to request this permission at the last moment).
Before requesting permission I check if the user has not already granted it. Which led me to the following construction:
Function for permissions checking:
function checkPermissions(perms, callback, failCallback) {
FB.api('/me/permissions', function (response) {
var fbPerms = response.data[0];
var haveAllPermissions = true;
if (typeof perms === 'string') {
perms = [ perms ];
}
for (var i in perms) {
if (fbPerms[perms[i]] == null) {
haveAllPermissions = false;
break;
}
}
if (haveAllPermissions) {
callback();
} else {
failCallback();
}
});
}
The version "broken"
Use the following construction (activated by one click on a button):
// Verifica permissões
checkPermissions("publish_actions",
// Em caso de sucesso cria o request
generateRequest,
// Em caso de fracasso
function () {
// requisita a permissão
FB.login(function () {
// Verifica a permissão novamente
checkPermissions("publish_actions",
// Em caso de sucesso cria o request
generateRequest,
// Em caso de fracasso notifica o usuário
function () {
alert("Permissão negada");
// Reativa o botão para postar colagens
$("#gerarColagem").one('click', enviaColagem);
});
}, {scope: "publish_actions"});
});
You can see the code in action http://sfcb.7rtc.com (by clicking on the Post button).
The problem
As I’m checking for permission publish_actions
the pop-up is not directly linked to the click. Even if a click activates the flow, the call to FB.login
is actually attached to a callback
. Result: Browsers are blocking login pop-up.
The undesirable solution
I can skip the first check by permissions and force a flow of login
always (if the user has already assigned the permission everything happens silently):
FB.login(function () {
// Verifica a permissão novamente
checkPermissions("publish_actions",
// Em caso de sucesso cria o request
generateRequest,
// Em caso de fracasso notifica o usuário
function () {
alert("Permissão negada");
// Reativa o botão para postar colagens
$("#gerarColagem").one('click', enviaColagem);
});
}, {scope: "publish_actions"});
In this case (skipping the first permissions check) the pop-up normally opens once the method FB.login
is responding directly to the click. The downside is that it is calling the login method every time, causing users who have already secured permission to previously handle a overhead unnecessary and recurrent.
In the happy scenario the user will grant permission the first time they click on "Post". The "broken" version ensures that these users do not go through a login flow unnecessarily; everything works correctly after the first authorization.
So my question is: How to structure my calls to make and request permission with FB.login
without the browser blocking the Facebook pop-up? Is there any way to check permissions before calling the FB.login
without the browser blocking the pop-up?
Scenario Perfect
- The user clicks on Post
- The application checks that the user has the permission
publish_actions
- The application publishes the photo (without requesting login with extra permission)
Alternative scenario:
- The user clicks on Post
- b) The application checks that the user does not have the permission
publish_actions
- The application requests new login with permission (this window is not blocked by the browser)
- The user authorizes permission
- The application publishes the photo
With so many scenarios, was a little confused to understand the desired scenario, exactly as you want, could edit and put in much relevance your "perfect" scenario with the behavior exactly described?
– Paulo Roberto Rosa
Paulo, I updated it in use case format. The whole dilemma of the thing is that either I implement step 2 as described and step 3 of the alternative scenario breaks (due to pop-up blocker) or I skip step 2 and log in always (ruining the perfect scenario). It became clearer?
– Anthony Accioly