为什么这个Facebook的javascript-sdk登录代码自动记录用户一次又一次? [英] Why this Facebook javascript-sdk login code is automatically logging the user again and again?

查看:148
本文介绍了为什么这个Facebook的javascript-sdk登录代码自动记录用户一次又一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,用户按下我的网站上的注销按钮,我无法保持用户注销。他被重定向到登录页面,但是updateButton函数被再次调用,并且重新登录了相同的凭据。我尝试了几种方法,但问题仍然存在。我想我在updateButton功能下没有正确的操作,而且FB.logout()还没有正确完成,因为我在控制台中收到错误FB.logout call without access access。
代码如下: -

Here I am unable to keep a user logged out once the user presses the logout button from my site. He is redirected to the login page but updateButton function is called again with same credentials he is getting logged in again. I tried several ways but the problem persists. I guess I am not doing things rightly in updateButton function down here and also FB.logout() is not being done correctly, as I get the error "FB.logout called without an access token" in the console. The code is as follows:-

            $(function(){

           var button; 

           window.fbAsyncInit = function(){

            FB.init({ appId      : 'myAppId',

            status     : true,
            cookie     : true,
            xfbml      : true,
            oauth      : true
        });



         function updateButton(response) {// I am not sure I am doing it right here
             console.log("Update Button Fired.");
             console.log(response);

               button  = document.getElementById('fb-auth');

                if(response.status === 'connected')
                {
                    FB.api('/me', function(info)
                    {
                        login(response,info); 
                    });
                 } 
                else
                {
                     FB.login(function(response)
                     {
                            if(response.status === 'not_authorized')
                           {
                                FB.api('/me', function(info){
                                    login(response, info); 
                                });
                            } 
                            else
                            {

                            }  
                    }, {scope:'email, user_birthday,user_about_me' });
                }
            }  

          }

        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);

        };

        (function() {
            var e = document.createElement('script');
            e.async = true;
            e.type = 'text/javascript';
            e.src = document.location.protocol +  '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
        })();

        function login(response,info){
            console.log('login Showloader called');
            if (response.authResponse) { 
                var accessToken = response.authResponse.accessToken;
              $.post("/web/register/faceBookRegistration",{ data:info,accessTokenValue:accessToken}).done(                       function(data){
                            if(typeof(data) != undefined){
                                window.location = "/web/login/loadViewFaceLogin";

                            }
                        });
            }


        }
        function logout(response){
           FB.logout(function(response){
              console.log("User is now logged out"); 
           });
        }

      });  

另外我认为我的退出ie

Also I think my logout i.e

      function logout(response){ 
        FB.logout(function(response){
          console.log("User is now logged out"); 
    });
  }

不正确。在控制台中,它显示FB.logout没有访问令牌。可能原因

is not correct. In the console it shows that FB.logout called without an access token. What could be the reason

推荐答案

我自由地重写代码,保持基础:

I took the liberty to rewrite your code, keeping your bases:

   // File - fb-init.js

    window.fbAsyncInit = function() {
        FB.init({
            appId      : app_id,
            status     : false,
            cookie     : true,
            xfbml      : true
        });
    };

    (function(d){
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));

-

    // File - main.js

    $('#fb-login').on('click', function() {
        FB.getLoginStatus(function(responseStatus) {
            if (responseStatus.status === 'connected') {
                FB.api('/me', function(responseMe) {
                    if (!responseMe.id) 
                        return false;
                    var accessToken = responseMe.authResponse.accessToken;
                    $.post('/web/register/faceBookRegistration' {
                        data                : responseMe,
                        accessTokenValue    : accessToken
                    }).done(function(data) {
                        if (typeof(data) !== 'undefined')
                            window.location = '/web/login/loadViewFaceLogin';    
                    });   
                });
            }
            else if (responseStatus.status === 'not_authorized') {
                FB.login(function(responseLogin) {
                    FB.api('/me', function(responseMe) {
                    if (!responseMe.id) 
                            return false;
                        var accessToken = responseMe.authResponse.accessToken;
                        $.post('/web/register/faceBookRegistration' {
                            data                : responseMe,
                            accessTokenValue    : accessToken
                        }).done(function(data) {
                            if (typeof(data) !== 'undefined')
                                window.location = '/web/login/loadViewFaceLogin';    
                        });   
                    });
                }, {scope:'email, user_birthday,user_about_me' });    
            }
            else
                return false;
        });    
    });

    $('#fb-logout').on('click', function() {
        FB.logout(function(responseLogout) {

        });    
    });

这篇关于为什么这个Facebook的javascript-sdk登录代码自动记录用户一次又一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆