angular ui routing for authenticated users, variable is showing undefined even when intialized as false

I am modelling my routing based on this post: angular ui-router login authentication.

I am making a cordova phonegap app. I'm new to angularjs and it's quite a new learning experience so I appreciate any suggestions at all! If I'm building the authentication routing wrong please let me know.

I have 2 factories and a run method. My first factory is titled 'principal', and the most important variable is isFBLoggedIn. It also has has a return of 3 functions, 2 of which are intializers, one initializing quickblox and one initializing facebook. The 3rd function just resolves and returns the promise.

The second factory is named 'authorization' and really just redirects based on whether the variable isFBLoggedIn is true. It is resolved in my state named 'app' which is an abstract parent state. (It has a menu template and menu controller but not sure if that matters).

The run method simply sets the toState and toStateParams and then also routes based on isFBLoggedIn. I'm not sure if I needed to put routes here but I assumed I did since I believe factories only run once...

So my variable isFBLoggedIn is coming up undefined and I cant figure out why. I've initialized it in my code...

I tried to copy as much of the code from that link because I'm not very familiar with angularjs ui. However if you believe there is a better way that I should be authenticating please let me know. I'd love to do things properly the first time and not the 10th time...

I've been sitting/looking at this too long so I'm hoping I can get some help...What am I doing wrong?

Edit to add: In addition to the isFBLoggedIn variable showing up undefined, when I type the url into my browser for a page requiring login, I see the url in the browser changing to /login (which is what I want/expect). However the actual view does not change. I came across this link which had it seems the same issue. However I don't understand what they are saying is the problem...

run method

.run(['$rootScope','$state','$stateParams','principal','authorization',function($rootScope,$state,$stateParams,principal,authorization){

    $rootScope.$on('$stateChangeStart',function(event,toState,toParams,fromState,fromParams){
      $rootScope.toState = toState;
      $rootScope.toStateParams = toParams;

      // if logged in and going to login page, redirect to cards page
      if (principal.isFBLoggedIn && toState.name === 'app.login'){
        $state.go('app.cards');
      } else if (!principal.isFBLoggedIn && toState.name != 'app.login') {
        // not logged in and not going to login page, redirect to login page.
        $state.go('app.login');
      } else {
           // this is theoretically all fine but that's not true 
           // because things are being routed here because isFBLoggedIn is showing undefined.
      }

    });
  }])

principal factory

  .factory('principal',['$q','$timeout',function($q,$timeout){
    isFBLoggedIn = false;
    return {
      QBInit: function(){
        QB.init(CONFIG.application_id, CONFIG.authKey, CONFIG.authSecret, CONFIG.debug);
        QB.createSession(function(err, result) {
          if (err){
            return $q.reject("QB Create Session Error");
          } else {
            return result.token;
          }
        });
      },
      FBInit: function(){

        if (!window.cordova) {
          // first check if fb is defined and run browser init and get login status if successful
          checkFBDefined(function(){
            facebookConnectPlugin.browserInit(1506810102869030,"v2.0");
            facebookConnectPlugin.getLoginStatus(
              function(res){
                if (typeof(res) != 'undefined' && res.status === 'connected'){
                  this.isFBLoggedIn = true;
                }
                return res;
              },
              function(err){return $q.reject('getLoginStatus Error: ' + err);
            });
          },0,$timeout);

        } else {
            facebookConnectPlugin.getLoginStatus(
              function(res){
                if (typeof(res) != 'undefined' && res.status === 'connected'){
                  this.isFBLoggedIn = true;
                }
                return res;
              },
              function(err){return $q.reject('getLoginStatus Error: ' + err);
            });
        }
      },
      initialization: function(){
        var deferred = $q.defer();
        var promise = deferred.promise.then(this.QBInit).then(this.FBInit);
        deferred.resolve();
        return promise;
      }
    }
  }])

authorization factory

  .factory('authorization',['$rootScope','$state','principal',function($rootScope,$state,principal){
    return {
      authorize: function(){
        return principal.initialization().then(function(){
          if (principal.isFBLoggedIn && $rootScope.toState.name == 'app.login'){
            // logged in, but going to login page, redirect to cards page
            $state.go('app.cards');
          } else if (!principal.isFBLoggedIn && $rootScope.toState.name !='app.login'){
            // not logged in and not going to login page, redirect to login page.
            $rootScope.returnToState = $rootScope.toState;
            $rootScope.returnToStateParams = $rootScope.toStateParams;
            $state.go('app.login');
          } else {
            console.log("routes should be fine but isn't because isFBLoggedIn is undefined");
          }   

        });
      }
    }
  }])

where authorization factory is resolved:

.config(function($stateProvider, $urlRouterProvider) {
    $stateProvider

      .state('app', {
        url: "/app",
        abstract: true,
        templateUrl: "templates/menu.html",
        resolve:{authorize: ['authorization',function(authorization){
            return authorization.authorize();
            }]
        },
        controller: 'MenuCtrl'
      })

You init isFBLoggedIn = false; as global variable and it is not bound to factory principal you inject to app.run.

Try this:

.factory('principal',['$q','$timeout',function($q,$timeout){
  var isFBLoggedIn = false;
  return {
    isFBLoggedIn: isFBLoggedIn,
    QBInit: function(){ // rest of code

when you initialize isFBLoggedIn you have:

isFBLoggedIn = false;

I belive you want

this.isFBLoggedIn = false;