1

Help please, whats wrong in my code, because browser blocking pop up facebook window. I have no idea. In some browser its blocked, sometimes it is ok. I should to find out the workaround.

/*facebook oauth*/
$('#oauth_facebook').on('click', function (event) {
    event.preventDefault();
    window.fbAsyncInit = function () {

        FB.init({
            appId: 'ххххххххххх',
            cookie: true,
            xfbml: true,
            oauth: true
        });

        FB.getLoginStatus(function (response) {
            console.log('sucsess');
            if (response.status === 'connected') {
                var accessToken = response.authResponse.accessToken;
                var userID = response.authResponse.userID;
                var userName, userEmail;
                var url = '/me?fields=name,email,picture.width(300).height(300)';
                var avatar;
                FB.api(url, function (response) {
                    userName = response.name;
                    userEmail = response.email;
                    avatar = response.picture.data.url;
                  
                        $.ajax({
                            type: 'POST',
                            url: api+'login/oauth2',
                            data: {
                                avatar: avatar,
                                token: accessToken,
                            },
                            success: function (data) {
                              
                             
                            },
                            error: function (xhr, str) {
                                console.log(str);
                            }
                        });
                    
                });
            }
            else {
                FB.login(function (response) {
                    if (response.status === 'connected') {

                        var url = '/me?fields=name,email,picture.width(300).height(300)';
                        var userName, userEmail, accessToken, userID;
                        accessToken = response.authResponse.accessToken;
                        userID = response.authResponse.userID;

                        FB.api(url, function (response) {
                            userName = response.name;
                            userEmail = response.email;
                            avatar = response.picture.data.url;
                           
                                $.ajax({
                                    type: 'POST',
                                    url: api+'login/oauth2',
                                    data: {
                                        avatar: avatar,
                                        token: accessToken,
                                    },
                                    success: function (data) {
                        
                                    },
                                    error: function (xhr, str) {
                                        console.log(str);
                                    }
                                });
                            }

                        });//api FB
                    

                }, {scope: 'email'});
            }
        });
    };
    (function () {
        var e = document.createElement('script');
        e.async = true;
        e.src = document.location.protocol +
        '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
});
/*facebook oauth*/

Help please, whats wrong in my code, because browser blocking pop up facebook window. I have no idea. In some browser its blocked, sometimes it is ok. I should to find out the workaround.

lemmik12
  • 33
  • 5
  • 1
    Possible duplicate of [Avoid browser popup blockers](http://stackoverflow.com/questions/2587677/avoid-browser-popup-blockers) – jeffdill2 Dec 04 '15 at 14:19
  • 2
    You need to call `FB.login` _directly_ on a user interaction (usually a click somewhere) – you can not nest it into other functions that work asynchronously (such as `FB.getLoginStatus`) – that will lose the “connection” between the user’s click and the attempt to open a popup, and therefor it will be blocked. I’d suggest that you embed and initialize the SDK, and check the user’s login status outside of your click handler, on page load already. Then you can call `FB.login` directly in your click handler if necessary. – CBroe Dec 04 '15 at 15:00
  • Ok, thank you, therefore how is my code should be look? – lemmik12 Dec 04 '15 at 15:18

0 Answers0