<commit_before>/*
 * Author: <NAME>
 * Contains an Authentication Service that provides functionality such as checking if
 * a user is logged in, logs a user in and out, and a static function that safeguards
 * protected states from unauthenticated users. Contains a service that stores the
 * user object returned from the DB, and checking if a user is authenticated is done
 * by checking the status of SessionService.user. 
 * Also contains an app constant called AUTH_EVENTS, which should be used to broadcast
 * auth-related events app wide like: $rootScope.$on('$some-auth-event-name', ...)
 * Will be useful for allowing/denying access to parts of the app when user permissions
 * are implemented.
 */
require('angular').module('ays')
  .constant('AUTH_EVENTS', {
    loginSuccess: 'auth-login-success',
    loginFailed: 'auth-login-failed',
    logoutSuccess: 'auth-logout-success',
    loginWhileSessionValid: 'auth-login-with-valid-session',
    notAuthenticated: 'auth-not-authenticated', // this is broadcasted when a user isn't logged in
    notAuthorized: 'auth-not-authorized',
  })
  .factory('AuthService', 
    ['$localStorage', '$sails', 'SessionService', 'HttpPendingRequestsService', '$rootScope', '$state',
    function ($localStorage, $sails, SessionService, HttpPendingRequestsService, $rootScope, $state) {
    var AuthService = {
      getUserAuthStatus: function () {
        // $http/$sails returns a promise, which has a then function, which also returns a promise
        var promise = $sails.get('/api/me')
          .then(function (response) {
            SessionService.user = response.data;
            return response;
          })
          .catch(function (error) {
            return error;
          });
        return promise;
      },
      login: function (credentials) {
        var promise = $sails.post('/api/login', credentials)
          .then(function (response) {
            SessionService.user = response.data;
            return response;
          })
          .catch(function (error) {
            return error;
          });
        return promise;
      },
      isAuthenticated: function () {
        return !!SessionService.user;
      },
      logout: function () {
        var promise = $sails.get('/api/logout')
          .then(function (response) {
            return response;
          })
          .catch(function (error) {
            return error;
          });
        SessionService.destroy();// reset saved user info in front-end no matter what
        return promise;
      },
      initStateGuard: function () {

        // run auth check for each url visited
        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
          $localStorage.put('last_visited', toState.name);
          // console.log($localStorage.get('last_visited'));
          if(!fromState.views) {
            console.log($localStorage.get('last_visited'))
            // $state.go($localStorage.get('last_visited'));
          }
          AuthService.getUserAuthStatus().then(function (response) {
            if (response.status === 401) {
              if (toState.protected) {
                event.preventDefault();
                HttpPendingRequestsService.cancelAll(); // prevent all http requests
                // if user isnt authenticated and attempts to view a protected state,
                // redirect to login and store the desired view to redirect them back after
                $state.go('login', {toState: toState.name, toParams: toParams});
              }
            } else if (response.status === 200) {
              // if user is authenticated, dont make them log in again
              if (toState.url === '/login') {
                event.preventDefault();
                $state.transitionTo('intro');
              }
            }
          })
        });
      },
      initAppLoad: function () {
        // set up Session Service on app load
        AuthService.getUserAuthStatus().then(function (response) {
          if (response.status === 401) {
            SessionService.user = null;
          } else if (response.status === 200) {
            SessionService.user = response.data;
            $state.go('intro');// redirect user straight to /intro if they are still logged in
          }
        });
      },
      recordStateHistory: function () {
        $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
          
        })
      }
    };
    return AuthService;
  }])
  .service('SessionService', function () {
    this.create = function (user) {
      this.user = user;
    };
    this.destroy = function () {
      this.user = null;
    };
  });<commit_msg>#69 Defect: Fix + Improve handling browser refresh<commit_after>/*
 * Author: <NAME>
 * Contains an Authentication Service that provides functionality such as checking if
 * a user is logged in, logs a user in and out, and a static function that safeguards
 * protected states from unauthenticated users. Contains a service that stores the
 * user object returned from the DB, and checking if a user is authenticated is done
 * by checking the status of SessionService.user.
 * Also contains an app constant called AUTH_EVENTS, which should be used to broadcast
 * auth-related events app wide like: $rootScope.$on('$some-auth-event-name', ...)
 * Will be useful for allowing/denying access to parts of the app when user permissions
 * are implemented.
 */
require('angular').module('ays')
  .constant('AUTH_EVENTS', {
    loginSuccess: 'auth-login-success',
    loginFailed: 'auth-login-failed',
    logoutSuccess: 'auth-logout-success',
    loginWhileSessionValid: 'auth-login-with-valid-session',
    notAuthenticated: 'auth-not-authenticated', // this is broadcasted when a user isn't logged in
    notAuthorized: 'auth-not-authorized',
  })
  .factory('AuthService',
    ['$localStorage', '$sails', 'SessionService', 'HttpPendingRequestsService', '$rootScope', '$state',
    function ($localStorage, $sails, SessionService, HttpPendingRequestsService, $rootScope, $state) {
    var AuthService = {
      getUserAuthStatus: function () {
        // $http/$sails returns a promise, which has a then function, which also returns a promise
        var promise = $sails.get('/api/me')
          .then(function (response) {
            SessionService.user = response.data;
            return response;
          })
          .catch(function (error) {
            return error;
          });
        return promise;
      },
      login: function (credentials) {
        var promise = $sails.post('/api/login', credentials)
          .then(function (response) {
            SessionService.user = response.data;
            return response;
          })
          .catch(function (error) {
            return error;
          });
        return promise;
      },
      isAuthenticated: function () {
        return !!SessionService.user;
      },
      logout: function () {
        var promise = $sails.get('/api/logout')
          .then(function (response) {
            return response;
          })
          .catch(function (error) {
            return error;
          });
        SessionService.destroy();// reset saved user info in front-end no matter what
        return promise;
      },
      initStateGuard: function () {

        // run auth check for each url visited
        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
          $localStorage.put('to_state_name', toState.name);
          $localStorage.put('from_state_name', fromState.name);
          AuthService.getUserAuthStatus().then(function (response) {
            if (response.status === 401) {
              if (toState.protected) {
                event.preventDefault();
                HttpPendingRequestsService.cancelAll(); // prevent all http requests
                // if user isnt authenticated and attempts to view a protected state,
                // redirect to login and store the desired view to redirect them back after
                $state.go('login', {toState: toState.name, toParams: toParams});
              }
            } else if (response.status === 200) {
              // if user is authenticated, dont make them log in again
              if (toState.url === '/login') {
                event.preventDefault();
                $state.transitionTo('intro');
              }
            }
          })
        });
      },
      initAppLoad: function () {
        // Handles browser refreshes with localstorage. If there's no from_state_name saved from the
        // initStateGuard method, then the app was just loaded.
        if($localStorage.get('from_state_name') === '') {
          $state.go($localStorage.get('to_state_name'));
        } else {
          // set up Session Service on app load
          AuthService.getUserAuthStatus().then(function (response) {
            if (response.status === 401) {
              SessionService.user = null;
            } else if (response.status === 200) {
              SessionService.user = response.data;
              $state.go($localStorage.get('from_state_name'));// redirect user straight to /intro if they are still logged in
            }
          });
        }
      },
      recordStateHistory: function () {
        $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {

        })
      }
    };
    return AuthService;
  }])
  .service('SessionService', function () {
    this.create = function (user) {
      this.user = user;
    };
    this.destroy = function () {
      this.user = null;
    };
  });
{'fraction_non_alphanumeric': 0.11429159802306425, 'fraction_numerical': 0.0026771004942339376, 'mean_word_length': 2.2077278731836194, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '623515', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2480, 'n_words': 737}