<commit_before>/// <reference path='..\libs\angular\angular.d.ts' />
/// <reference path='..\libs\jquery\jquery.d.ts' />
/// <reference path='..\libs\angular\angular-route.d.ts'/>

/// <reference path='components/home/homeController.ts' />
/// <reference path='components/navbar/navbarController.ts' />
/// <reference path='components/signup/signupController.ts' />
/// <reference path='components/dashboard/dashboardController.ts' />


 // http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory
angular.module('app', ['app.controllers','ngRoute','ngStorage', 'ngResource'])

.config(function ($routeProvider, $locationProvider, $httpProvider) {

    "use strict"; // do I need this?

    $routeProvider
      .when('/home', {
        templateUrl:    '\\views\\homeView.html',
        controller:     'Home.Controller',
        // Error: [$injector:unpr] Unknown provider: AccessProvider <- Access <- access
        resolve: {
          access: ["Access", (Access) => {
            console.log(Access);
          }]
        }
      })
      .when('/signup', {
        templateUrl:    '\\views\\signupView.html',
        controller:     'Signup.Controller',
        resolve: {
          /*access: ["Access", function(Access) { return Access.isAnonymous(); }],*/
        }
      })
      .when('/dashboard', {
        templateUrl:    '\\views\\dashboardView.html',
        controller:      'Dashboard.Controller',
        resolve: {
          access: ["Access", (Access) => {
            return Access.isAuthenticated();
          }]
          //["Access", function(Access) { return Access.isAuthenticated(); }],
        }
      })
      .otherwise( {
        redirectTo:     '/home',
        controller:     'Home.Controller',
      });
})

// User is a standard AngularJS resource with a profile method (needs angular-resource):
.factory("User", ["$resource", function($resource) {
  // return AuthenticationService.User.Factory($resource);
  this.user = $resource("rest/users/:id/:attr", { id: "@id" }, {

          profile: {
            method: "GET",
            params: { attr: "profile" }
          }
        });
      return this.user;
}])
// UserProfile encapsulates the current user to implement the
// isAnonymous and isAuthenticated methods logic:
.factory("UserProfile", ["$q", "User", function($q, User) {
    // return AuthenticationService.UserProfile.Factory($q, User);
    var deferred = $q.defer();

      console.log("in UserProfile");
        User.profile(function(userProfile) {
          console.log("Starting resolve from UserProfile");
          deferred.resolve({
            isAnonymous: function() {
              console.log("anonymous from userProfile");
              return userProfile.anonymous;
            },

            isAuthenticated: function() {
              console.log("Authenticated from userProfile");
              return !userProfile.anonymous;
            }

          });
        });

        return deferred.promise;
}])
.factory("Access", ["$q", "UserProfile", function($q, UserProfile) {
  // return AuthenticationService.Access.Factory($q, UserProfile);
  var Access = {

        OK: 200,
        UNAUTHORIZED: 401,
        FORBIDDEN: 403,

        isAnonymous: function() {
          var deferred = $q.defer();
          console.log("Starting promise from isAnonymous");
          UserProfile.then(function(userProfile) {
            if (userProfile.isAnonymous()) {
              deferred.resolve(Access.OK);
              console.log("Access OK from isAnonymous");
            } else {
              deferred.reject(Access.FORBIDDEN);
              console.log("Access FORBIDDEN from isAnonymous");
            }
          });
          return deferred.promise;
        },

        isAuthenticated: function() {
          var deferred = $q.defer();
          console.log("Starting promise from isAuthenticated");
          UserProfile.then(function(userProfile) {
            if (userProfile.isAuthenticated()) {
              deferred.resolve(Access.OK);
              console.log("Access OK from isAuthenticated");
            } else {
              deferred.reject(Access.UNAUTHORIZED);
              console.log("Access UNAUTHORIZED from isAuthenticated");
            }
          });
          return deferred.promise;
        }

      };
      console.log("returning access");
      return Access;
}])

// when Access rejects a promise, the $routeChangeError event will be fired:
.run(["$rootScope", "Access", "$location",
function($rootScope, Access, $location) {

  $rootScope.$on("$routeChangeError", function(event, current, previous, rejection) {
    console.log("routeChangeError is triggered");
    if (rejection == Access.UNAUTHORIZED) {
      console.log("rejection == Access.UNAUTHORIZED");
      $location.path("/home");
    } else if (rejection == Access.FORBIDDEN) {
      console.log("rejection == Access.FORBIDDEN");
      $location.path("/home");
    }
  });<commit_msg>Testing to see if promises are the issue<commit_after>/// <reference path='..\libs\angular\angular.d.ts' />
/// <reference path='..\libs\jquery\jquery.d.ts' />
/// <reference path='..\libs\angular\angular-route.d.ts'/>

/// <reference path='components/home/homeController.ts' />
/// <reference path='components/navbar/navbarController.ts' />
/// <reference path='components/signup/signupController.ts' />
/// <reference path='components/dashboard/dashboardController.ts' />


 // http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory
angular.module('app', ['app.controllers','ngRoute','ngStorage', 'ngResource'])

.config(function ($routeProvider, $locationProvider, $httpProvider) {

    "use strict"; // do I need this?

    $routeProvider
      .when('/home', {
        templateUrl:    '\\views\\homeView.html',
        controller:     'Home.Controller',
        // Error: [$injector:unpr] Unknown provider: AccessProvider <- Access <- access
        resolve: {
          access: ["Access", (Access) => {
            console.log(Access);
          }]
        }
      })
      .when('/signup', {
        templateUrl:    '\\views\\signupView.html',
        controller:     'Signup.Controller',
        resolve: {
          access: ["Access", function(Access) { return Access.isAnonymous(); }],
        }
      })
      .when('/dashboard', {
        templateUrl:    '\\views\\dashboardView.html',
        controller:      'Dashboard.Controller',
        resolve: {
          access: ["Access", function(Access) { return Access.isAuthenticated(); }]
          // ["Access", (Access) => { return Access.isAuthenticated();}]
        }
      })
      .otherwise( {
        redirectTo:     '/home',
        controller:     'Home.Controller',
      });
})

// User is a standard AngularJS resource with a profile method (needs angular-resource):
.factory("User", ["$resource", function($resource) {
  // return AuthenticationService.User.Factory($resource);
  this.user = $resource("rest/users/:id/:attr", { id: "@id" }, {

          profile: {
            method: "GET",
            params: { attr: "profile" }
          }
        });
      return this.user;
}])
// UserProfile encapsulates the current user to implement the
// isAnonymous and isAuthenticated methods logic:
.factory("UserProfile", ["$q", "User", function($q, User) {
    // return AuthenticationService.UserProfile.Factory($q, User);
    var deferred = $q.defer();

      console.log("in UserProfile");
        User.profile(function(userProfile) {
          console.log("Starting resolve from UserProfile");
          deferred.resolve({
            isAnonymous: function() {
              console.log("anonymous from userProfile");
              return userProfile.anonymous;
            },

            isAuthenticated: function() {
              console.log("Authenticated from userProfile");
              return !userProfile.anonymous;
            }

          });
        });
        console.log("returning from userProfile");
        return deferred.promise;
}])
.factory("Access", ["$q", "UserProfile", function($q, UserProfile) {
  // return AuthenticationService.Access.Factory($q, UserProfile);
  var Access = {

        OK: 200,
        UNAUTHORIZED: 401,
        FORBIDDEN: 403,

        console.log("Passed in promise is" + $q);
        isAnonymous: function() {
          var deferred = $q.defer();
          console.log("Starting promise from isAnonymous");
          console.log("deferred is " + deferred);
          UserProfile.then(function(userProfile) {
            if (userProfile.isAnonymous()) {
              deferred.resolve(Access.OK);
              console.log("Access OK from isAnonymous");
            } else {
              deferred.reject(Access.FORBIDDEN);
              console.log("Access FORBIDDEN from isAnonymous");
            }
          });
          return deferred.promise;
        },

        //so far what it prints out when a use trys to log in and it is succesful is 
        //Starting promise from isAuthenticated
        //deferred is [object Object]
        //meaning that the promise isn't getting passed in correctly 
        isAuthenticated: function() {
          var deferred = $q.defer();
          console.log("Starting promise from isAuthenticated");
          console.log("deferred is " + deferred);
          UserProfile.then(function(userProfile) {
            if (userProfile.isAuthenticated()) {
              deferred.resolve(Access.OK);
              console.log("Access OK from isAuthenticated");
            } else {
              deferred.reject(Access.UNAUTHORIZED);
              console.log("Access UNAUTHORIZED from isAuthenticated");
            }
          });
          return deferred.promise;
        }

      };
      console.log("returning access");
      return Access;
}])

// when Access rejects a promise, the $routeChangeError event will be fired:
.run(["$rootScope", "Access", "$location",
function($rootScope, Access, $location) {

  $rootScope.$on("$routeChangeError", function(event, current, previous, rejection) {
    console.log("routeChangeError is triggered");
    if (rejection == Access.UNAUTHORIZED) {
      console.log("rejection == Access.UNAUTHORIZED");
      $location.path("/home");
    } else if (rejection == Access.FORBIDDEN) {
      console.log("rejection == Access.FORBIDDEN");
      $location.path("/home");
    }
  });
{'fraction_non_alphanumeric': 0.1480839591758136, 'fraction_numerical': 0.003273637589062199, 'mean_word_length': 2.311125278928913, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4580896', 'n_tokens_mistral': 2934, 'n_tokens_neox': 2802, 'n_words': 582}