Prevent pop-up blockers that appear in facebook sdk

A have problems blocking pop-ups that appear on the Facebook JS SDK. After reading the documentation, I understand that the JS SDK call should be after the user clicks. This code works correctly, and the browser does not block the popup:

function Login() { FB.login(function (response) { if (response.authResponse) { DoSmth(response.authResponse.accessToken); } }, { scope:'user_birthday, publish_stream' }); } 

But if I use this code anywhere (for example, on the wall, get user data), a pop-up window will be displayed in all cases, even if the user has already registered with facebook. I think this is not normal.

To determine that the user is already logged in, I use this code:

 function CheckOrLogin() { FB.getLoginStatus(function (response) { if (response.status === 'connected') { DoSmth(response.authResponse.accessToken); } else { FB.login(function (response) { if (response.authResponse) { DoSmth(response.authResponse.accessToken); } }, { scope:'user_birthday, publish_stream' }); } }, true); } 

In this case, the pop-up window will not be displayed if the user is already logged in. But if the user was not logged into facebook, this condition (if (response.status ===) returns false. And the browser will be BLOCK , which will be shown by FB.login. Is there any way to prevent this?

+4
source share
1 answer

Try to check the login status immediately upon loading the page and save the result and access Token in some variable.

 var userData = {isConnected: false, token: null}; FB.getLoginStatus(function (response) { userData.isConnected = (response.status === 'connected'); if (userData.isConnected) { userData.token = response.authResponse.accessToken; } }, true); 

When the user clicks on a button, just check the value of the variable.

 function CheckOrLogin() { if (userData.isConnected) { DoSmth(userData.token); } else { FB.login(function (response) { if (response.authResponse) { DoSmth(response.authResponse.accessToken); } }, { scope:'user_birthday, publish_stream' }); } } 
0
source

Source: https://habr.com/ru/post/1403108/


All Articles