FB.logout问题,FB.getLoginStatus()返回未知状态

时间:2016-08-13 11:41:41

标签: angularjs facebook facebook-javascript-sdk

我试图让FB.logout()功能正常工作。

然后我得到的错误是在没有访问令牌的情况下调用FB.logout()。 所以我搜索了一些解决方案。所以我发现我们必须得到状态然后尝试。所以当我尝试getLoginStatus()时。 authResponse为空,状态未知。

所以我不知道我没有得到authResponse的原因。

我附上了我的代码。

Controller.js

app.controller("dashboardCtrl",["$scope","authFactory","$location",function($scope,authFactory,$location){
var userObj = authFactory.getUserObj(userObj);
console.log(userObj);

var accessToken = authFactory.getAccessToken();
console.log(accessToken);
$scope.accessToken = accessToken;

$scope.FBout = function(){

    FB.getLoginStatus(function(response) {
        console.log(response);
        if (response.status === 'connected') {

            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;

            FB.logout(function(response) {
                authFactory.clearCookie();

            });
        } else if (response.status === 'not_authorized') {

        } else {
        }
    });
};
}]);

factory.js

 app.factory("authFactory",["$cookies","$location",function($cookies,$location){
var authFactory = {};

authFactory.setAccessToken = function(accessToken){
    $cookies.put("accessToken",accessToken);
}

authFactory.getAccessToken = function(){
    return $cookies.get("accessToken");
}

authFactory.getUserObj = function(){
    var userObj = $cookies.get('userObj');

    if(userObj){
        return userObj;
    }
    else {
        console.log("error");
    }
}

authFactory.clearCookie = function(){
    $cookies.remove("accessToken");
    $location.path("/")


}
return authFactory;
}]);

我无法注销并重定向到其他页面。我被卡住了,我有accessstoken,以及cookie中的用户对象。

我确信我已取消选中Chrome设置中的第三方应用访问权限。

使用我登录的其他控制器进行更新,这可能是必需的。

app.controller('homeCtrl',["$scope","authFactory","$location","$cookieStore",function($scope,authFactory,$location,$cookieStore){
$scope.name = "login Please";

$scope.FBLogin = function(){

    FB.login(function(response) {
if (response.authResponse) {
 console.log('Welcome!  Fetching your information.... ');
 FB.api('/me', function(response) {

   console.log('Good to see you, ' + response.name + '.');
   $cookieStore.put("userObj",response);
   var accessToken =  FB.getAuthResponse().accessToken;
  authFactory.setAccessToken(accessToken);

  $location.path("/dashboard");
  $scope.$apply();
 });
} else {
 console.log('User cancelled login or did not fully authorize.');
}
});

};

}]);

这是我的Route.js

app.config(["$routeProvider",function($routeProvider){
$routeProvider
    .when("/",{
        templateUrl : 'views/home/login.html',
        controller : 'homeCtrl'
    })

    .when("/dashboard",{
        templateUrl : 'views/home/dashboard.html',
        controller : 'dashboardCtrl',
        authenticated : true
    })

    .otherwise('/',{
        templateUrl : 'views/home/login.html',
        controller : 'homeCtrl'
    })

}]);

app.run(["$rootScope","$location","authFactory",function($rootScope,$location,authFactory){
$rootScope.$on('$routeChangeStart',function(event,next,current){
    if(next.$$route.authenticated){
        var userAuth = authFactory.getAccessToken();
        if(!userAuth){
            $location.path('/');
        }
    }
});
}]);

2 个答案:

答案 0 :(得分:0)

尝试在var parinput=document.createElement('input'); parinput.type="text"; parinput.name="package_location[]";中禁用缓存:

FB.getLoginStatus

更多信息:https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus#servers

答案 1 :(得分:0)

我遇到了同样的问题,请尝试以下操作:

FB.api(
  '/me/permissions',
  'DELETE',
  {},
  function(response) {
      // Here fb should remove the token.
      // response should be "success: true"
      FB.logout(() => {
               // Here logout.
      });
  }
);