<commit_before>;(function() {
  'use strict';
  /**
   * @ngdoc service
   * @function
   * @name ehaCouchDbService
   * @module eha.couchdb-auth
   */
  var ngModule = angular
  .module('eha.couchdb-auth.auth.service', [
    'restangular',
    'LocalForageModule',
    'ngCookies'
  ]);

  function CouchDbAuthService(options,
                              Restangular,
                              $log,
                              $q,
                              $localForage,
                              $rootScope) {

    var currentUser;

    // Create a new 'isolate scope' so that we can leverage and wrap angular's
    // sub/pub functionality rather than rolling something ourselves
    var eventBus = $rootScope.$new(true);

    function getSession() {
      return $q.when(Restangular
                      .all('_session')
                      .customGET());
    }

    function signIn(user) {
      return $q.when(Restangular
        .all('_session')
        .customPOST({
          name: user.username,
          password: <PASSWORD>
        }))
        .then(setCurrentUser)
        .then(function(user) {
          return getSession()
                  .then(function() {
                    return user;
                  });
        })
        .then(function(user) {
          if (!user || !user.ok) {
            $log.log('couchdb:login:failure:unknown');
            return $q.reject(new Error());
          }
          eventBus.$broadcast('authenticationStateChange');
          $log.log('couchdb:login:success', user);
          return decorateUser(user);
        })
        .catch(function(err) {
          if (err.status === 401) {
            $log.log('couchdb:login:failure:invalid-credentials', err);
            return $q.reject(new Error('Invalid Credentials'));
          } else {
            $log.log('couchdb:login:failure:unknown', err);
            return $q.reject(new Error(err));
          }
        });
    }

    function clearLocalUser() {
      currentUser = null;
      return $localForage.removeItem('user');
    }

    function setLocalUser(user) {
      return $localForage.setItem('user', user);
    }

    function getLocalUser() {
      return $localForage.getItem('user');
    }

    function signOut() {
      return $q.when(Restangular
        .all('_session')
        .remove())
        .then(clearLocalUser)
        .finally(function() {
          eventBus.$broadcast('authenticationStateChange');
        });
    }

    function resetPassword(config) {
      if (config.token && config.password) {
        return $q.when(Restangular
                       .all('reset-password')
                       .customPOST({
                         token: config.token,
                         password: <PASSWORD>
                       }));
      }

      if (config.email && config.callbackUrl) {
        return $q.when(Restangular
                       .all('reset-password')
                       .customPOST({
                         email: config.email,
                         callbackUrl: config.callbackUrl
                       }));
      } else {
        return $q.reject('You must provide both email and callbackUrl ' +
                         'properties in the payload');
      }

    }

    function addAccount() {
      return $q.reject('NOT_IMPLEMENTED');
    }

    function updateAccount() {
      return $q.reject('NOT_IMPLEMENTED');
    }

    function removeAccount() {
      return $q.reject('NOT_IMPLEMENTED');
    }

    function decorateUser(user) {
      user.hasRole = function(role) {
        var self = this;
        if (angular.isArray(role)) {
          var matches = role.filter(function(r) {
            return self.roles.indexOf(r) > -1;
          });
          return !!matches.length;
        } else if (angular.isString(role)) {
          return this.roles.indexOf(role) > -1;
        }
      };

      user.isAdmin = function() {
        return this.hasRole(options.adminRoles);
      };
      return user;
    }

    function getCurrentUser() {
      if (currentUser) {
        return $q.when(decorateUser(currentUser));
      }

      return getLocalUser()
        .then(function(user) {
          if (user) {
            currentUser = user;
            return decorateUser(user);
          } else {
            return $q.reject('User not found');
          }
        })
        .then(function(user) {
          return getSession()
            .then(function() {
              return user;
            });
        })
        .catch(function(err) {
          return $q.reject(err);
        });
    }

    function setCurrentUser(user) {
      if (user) {
        user = user.plain();
        currentUser = {
          name: user.name,
          roles: user.roles,
          bearerToken: user.bearerToken
        };
        return setLocalUser(user);
      }

      $q.reject('No user found');
    }

    return {
      signIn: signIn,
      signOut: signOut,
      resetPassword: resetPassword,
      accounts: {
        add: addAccount,
        update: updateAccount,
        remove: removeAccount
      },
      getSession: getSession,
      getCurrentUser: getCurrentUser,
      on: eventBus.$on.bind(eventBus),
      trigger: eventBus.$broadcast.bind(eventBus)
    };
  }

  ngModule.provider('ehaCouchDbAuthService',
  function ehaCouchDbAuthService($localForageProvider,
                                 ehaCouchDbAuthHttpInterceptorProvider,
                                 $httpProvider) {

    var options = {
      localStorageNamespace: 'eha',
      localStorageStoreName: 'auth',<commit_msg>refactor(login): merge session info into user<commit_after>;(function() {
  'use strict';
  /**
   * @ngdoc service
   * @function
   * @name ehaCouchDbService
   * @module eha.couchdb-auth
   */
  var ngModule = angular
  .module('eha.couchdb-auth.auth.service', [
    'restangular',
    'LocalForageModule',
    'ngCookies'
  ]);

  function CouchDbAuthService(options,
                              Restangular,
                              $log,
                              $q,
                              $localForage,
                              $rootScope) {

    var currentUser;

    // Create a new 'isolate scope' so that we can leverage and wrap angular's
    // sub/pub functionality rather than rolling something ourselves
    var eventBus = $rootScope.$new(true);

    function getSession() {
      return $q.when(Restangular
                      .all('_session')
                      .customGET());
    }

    function signIn(user) {
      return $q.when(Restangular
        .all('_session')
        .customPOST({
          name: user.username,
          password: <PASSWORD>
        }))
        .then(function(user) {
          return user.plain();
        })
        .then(setCurrentUser)
        .then(function(user) {
          return getSession()
                  .then(function(userMeta) {
                    return angular.extend(userMeta.plain(), user);
                  });
        })
        .then(setCurrentUser)
        .then(function(user) {
          if (!user || !user.ok) {
            $log.log('couchdb:login:failure:unknown');
            return $q.reject(new Error());
          }
          eventBus.$broadcast('authenticationStateChange');
          $log.log('couchdb:login:success', user);
          return decorateUser(user);
        })
        .catch(function(err) {
          if (err.status === 401) {
            $log.log('couchdb:login:failure:invalid-credentials', err);
            return $q.reject(new Error('Invalid Credentials'));
          } else {
            $log.log('couchdb:login:failure:unknown', err);
            return $q.reject(new Error(err));
          }
        });
    }

    function clearLocalUser() {
      currentUser = null;
      return $localForage.removeItem('user');
    }

    function setLocalUser(user) {
      return $localForage.setItem('user', user);
    }

    function getLocalUser() {
      return $localForage.getItem('user');
    }

    function signOut() {
      return $q.when(Restangular
        .all('_session')
        .remove())
        .then(clearLocalUser)
        .finally(function() {
          eventBus.$broadcast('authenticationStateChange');
        });
    }

    function resetPassword(config) {
      if (config.token && config.password) {
        return $q.when(Restangular
                       .all('reset-password')
                       .customPOST({
                         token: config.token,
                         password: <PASSWORD>
                       }));
      }

      if (config.email && config.callbackUrl) {
        return $q.when(Restangular
                       .all('reset-password')
                       .customPOST({
                         email: config.email,
                         callbackUrl: config.callbackUrl
                       }));
      } else {
        return $q.reject('You must provide both email and callbackUrl ' +
                         'properties in the payload');
      }

    }

    function addAccount() {
      return $q.reject('NOT_IMPLEMENTED');
    }

    function updateAccount() {
      return $q.reject('NOT_IMPLEMENTED');
    }

    function removeAccount() {
      return $q.reject('NOT_IMPLEMENTED');
    }

    function decorateUser(user) {
      user.hasRole = function(role) {
        var self = this;
        if (angular.isArray(role)) {
          var matches = role.filter(function(r) {
            return self.roles.indexOf(r) > -1;
          });
          return !!matches.length;
        } else if (angular.isString(role)) {
          return this.roles.indexOf(role) > -1;
        }
      };

      user.isAdmin = function() {
        return this.hasRole(options.adminRoles);
      };
      return user;
    }

    function getCurrentUser() {
      if (currentUser) {
        return $q.when(decorateUser(currentUser));
      }

      return getLocalUser()
        .then(function(user) {
          if (user) {
            currentUser = user;
            return decorateUser(user);
          } else {
            return $q.reject('User not found');
          }
        })
        .then(function(user) {
          return getSession()
            .then(function() {
              return user;
            });
        })
        .catch(function(err) {
          return $q.reject(err);
        });
    }

    function setCurrentUser(user) {
      if (user) {
        currentUser =  user;
        return setLocalUser(user);
      }

      $q.reject('No user found');
    }

    return {
      signIn: signIn,
      signOut: signOut,
      resetPassword: <PASSWORD>,
      accounts: {
        add: addAccount,
        update: updateAccount,
        remove: removeAccount
      },
      getSession: getSession,
      getCurrentUser: getCurrentUser,
      on: eventBus.$on.bind(eventBus),
      trigger: eventBus.$broadcast.bind(eventBus)
    };
  }

  ngModule.provider('ehaCouchDbAuthService',
  function ehaCouchDbAuthService($localForageProvider,
                                 ehaCouchDbAuthHttpInterceptorProvider,
                                 $httpProvider) {

    var options = {
      localStorageNamespace: 'eha',
      localStorageStoreName: 'auth',
{'fraction_non_alphanumeric': 0.1180598738562672, 'fraction_numerical': 0.000888336146397797, 'mean_word_length': 1.5720813342472013, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7107712', 'n_tokens_mistral': 3125, 'n_tokens_neox': 3047, 'n_words': 568}