0
votes

I am trying to get the FB.logout() functionality working.

Then error I get is FB.logout() called without an access token. so I googled around to find some solutions. So I found that we have to get the status and try then. So when I try to getLoginStatus(). the authResponse is Null, status is unknown.

So I don't know what's the reason that I am not getting the authResponse.

I am attaching my code.

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;
}]);

I am not able to logout and redirect to someother page. I am stuck, I have accesstoken, and user object in the cookie.

I am sure I have unchecked the thirdparty app access thing in chrome settings.

Updating with another controller where I logged in, which might be required.

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.');
}
});

};

}]);

this is my 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
Added the route,And the controller where the login was done - Pavan S

2 Answers

1
votes

Try to disable caching in FB.getLoginStatus:

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        FB.logout(function(response) {
            authFactory.clearCookie();
        });
    }
}, true);

More information: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus#servers

0
votes

I had the same problem, try this one:

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