<commit_before>var accountservices = angular.module('crmEngine.authservices',[]);
accountservices.factory('Auth', function($http) {
  var Auth = function(data) {
     angular.extend(this, data);
  };
  Auth.email = document.getElementById("userEmail").value;
  Auth.checkGapiToken = function(){
      var timeNow = new Date().getTime()/1000;
      var gapiToken = gapi.auth.getToken();
      if (gapiToken){
          var expirationTime = gapiToken.expires_at;
          var diff = expirationTime - timeNow;
          if (diff>0){
              return true;
          }
      }
      return false;
  }
  Auth.initWithLocalStorage = function(){
      console.log(Auth.email);
      var timeNow = new Date().getTime()/1000;
      // console.log('init with localStorage');
      if (localStorage.getItem("access_token")){
          // console.log('access token retreived from localStorage');
          // console.log(localStorage.getItem("access_token"));
          var access_token = localStorage.getItem("access_token");
          var authResultexpiration = localStorage.getItem("authResultexpiration");
          var diff = authResultexpiration - timeNow;
          if (diff>0 && access_token!="null"){
            //  console.log('the token is still valid it will expire in');
            //  console.log(diff);
             Auth.$scope.immediateFailed = false;
             Auth.$scope.isSignedIn = true;
             if (access_token!="null"){
                 gapi.auth.setToken({'access_token':access_token});
             }
            //  console.log('after setting gapi token');
            //  console.log(gapi.auth.getToken());
             window.authResult = {'access_token':access_token};
             Auth.$scope.runTheProcess();
          }
          else{
              // console.log('the token is expired');
              // console.log(diff);
              // refresh token
              Auth.refreshToken();
          }
      // render Google+ sign-in
      }else{
              // console.log('there is no access token on localStorage i will render signin');
              Auth.$scope.immediateFailed = true;
              
              if (typeof  Auth.$scope.apply() == 'function') { 
                  Auth.$scope.apply()
              }else{
                Auth.$scope.$apply();  
              }
              gapi.signin.render('myGsignin', {
                'callback': Auth.signIn,
                'clientid': '935370948155-a4ib9t8oijcekj8ck6dtdcidnfof4u8q.apps.googleusercontent.com',
                'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read',
                'theme': 'dark',
                'cookiepolicy': 'single_host_origin',
                'accesstype': 'online',
                'width':'wide'
              });

      }
  }
  Auth.goAhead = function(authResult){
      if (typeof(Storage) != "undefined") {
          localStorage['is_signed_in'] = true;
          localStorage['access_token']=authResult.access_token;
          localStorage['authResultexpiration'] = authResult.expires_at;
      }

      window.authResult = authResult;
      window.is_signed_in = true;
      window.access_token = authResult.access_token;
      window.authResultexpiration =  authResult.expires_at;

      Auth.$scope.runTheProcess();
  }
  Auth.initSimple = function(){
      var timeNow = new Date().getTime()/1000;
      if (window.is_signed_in){


          var diff = window.authResultexpiration - timeNow;
          if (diff>0){
             Auth.$scope.immediateFailed = false;
             Auth.$scope.isSignedIn = true;
             Auth.$scope.runTheProcess();
          }
          else{

              // refresh token
              Auth.refreshToken();

          }
      // render Google+ sign-in
      }else{
            // check the gapi token
            var isGapiOk = Auth.checkGapiToken();
            if (isGapiOk){
                var gapiToken = gapi.auth.getToken();
                Auth.processAuth(gapiToken);
            }else{
              gapi.signin.render('myGsignin', {
                'callback': Auth.signIn,
                'clientid': '935370948155-a4ib9t8oijcekj8ck6dtdcidnfof4u8q.apps.googleusercontent.com',
                'scope': 'https://www.googleapis.com/auth/userinfo.email',
                'theme': 'dark',
                'cookiepolicy': 'single_host_origin',
                'accesstype': 'online',
                'width':'wide'
              });
            }
      }
  }
  Auth.init = function($scope){
      // make sure there is only one instance of Auth.init executed
      if (!window.countInitExec){
          window.countInitExec = 1;
      }else{
          window.countInitExec = window.countInitExec+1;
          var timeNow = new Date().getTime()/1000;
          Auth.$scope = $scope;
          if (typeof(Storage) != "undefined") {
              // Using the localStorage
              Auth.initWithLocalStorage();
          } else {
              // Using the window object
              Auth.initSimple();
          }
      }
  };
  Auth.signIn = function(authResult){
      // console.log('now signedin i will show you th new authResult');
      // console.log(authResult);
      localStorage.removeItem('access_token');
      if (authResult.status.google_logged_in){
        gapi.auth.setToken(authResult);
        Auth.processAuth(authResult);
      }else{
        window.location.replace('/sign-in');
      }

  };
  Auth.processAuth = function(authResult) {
      //Auth.$scope.immediateFailed = true;
      // console.log(authResult);
      window.isRefreshing = false;
      if (authResult) {
        if (authResult['access_token']){
          Auth.$scope.immediateFailed = false;
          Auth.$scope.isSignedIn = true;
          Auth.goAhead(authResult);
        }
        else{
          // Auth.renderForcedSignIn();
          window.location.replace('/sign-in');
        }

      } else {
            Auth.renderForcedSignIn();
      };

  };
  Auth.renderForcedSignIn = function(){
    window.authResult = null;
    Auth.$scope.immediateFailed = true;
    gapi.signin.render('myGsignin', {
      'callback': Auth.signIn,
      'clientid': '935370948155-a4ib9t8oijcekj8ck6dtdcidnfof4u8q.apps.googleusercontent.com',
      'scope': 'https://www.googleapis.com/auth/userinfo.email',
      'theme': 'dark',
      'cookiepolicy': 'single_host_origin',
      'accesstype': 'online',
      'width':'wide'
    });
  }
  Auth.refreshToken = function(){
    if (!window.isRefreshing){
        window.isRefreshing = true;
        if (typeof Auth.$scope.apply() == 'function') { 
           Auth.$scope.apply();
        }else{
           Auth.$scope.$apply();
        }
        Auth.renderForcedSignIn();
    }
    //window.location.reload(true);

  };

  return Auth;
});<commit_msg>fix a bug on authservices<commit_after>var accountservices = angular.module('crmEngine.authservices',[]);
accountservices.factory('Auth', function($http) {
  var Auth = function(data) {
     angular.extend(this, data);
  };
  Auth.email = document.getElementById("userEmail").value;
  Auth.checkGapiToken = function(){
      var timeNow = new Date().getTime()/1000;
      var gapiToken = gapi.auth.getToken();
      if (gapiToken){
          var expirationTime = gapiToken.expires_at;
          var diff = expirationTime - timeNow;
          if (diff>0){
              return true;
          }
      }
      return false;
  }
  Auth.initWithLocalStorage = function(){
      console.log(Auth.email);
      var timeNow = new Date().getTime()/1000;
      // console.log('init with localStorage');
      if (localStorage.getItem("access_token")){
          // console.log('access token retreived from localStorage');
          // console.log(localStorage.getItem("access_token"));
          var access_token = localStorage.getItem("access_token");
          var authResultexpiration = localStorage.getItem("authResultexpiration");
          var diff = authResultexpiration - timeNow;
          if (diff>0 && access_token!="null"){
            //  console.log('the token is still valid it will expire in');
            //  console.log(diff);
             Auth.$scope.immediateFailed = false;
             Auth.$scope.isSignedIn = true;
             if (access_token!="null"){
                 gapi.auth.setToken({'access_token':access_token});
             }
            //  console.log('after setting gapi token');
            //  console.log(gapi.auth.getToken());
             window.authResult = {'access_token':access_token};
             Auth.$scope.runTheProcess();
          }
          else{
              // console.log('the token is expired');
              // console.log(diff);
              // refresh token
              Auth.refreshToken();
          }
      // render Google+ sign-in
      }else{
              // console.log('there is no access token on localStorage i will render signin');
              Auth.$scope.immediateFailed = true;
              
              Auth.$scope.$apply(); 
              gapi.signin.render('myGsignin', {
                'callback': Auth.signIn,
                'clientid': '935370948155-a4ib9t8oijcekj8ck6dtdcidnfof4u8q.apps.googleusercontent.com',
                'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read',
                'theme': 'dark',
                'cookiepolicy': 'single_host_origin',
                'accesstype': 'online',
                'width':'wide'
              });

      }
  }
  Auth.goAhead = function(authResult){
      if (typeof(Storage) != "undefined") {
          localStorage['is_signed_in'] = true;
          localStorage['access_token']=authResult.access_token;
          localStorage['authResultexpiration'] = authResult.expires_at;
      }

      window.authResult = authResult;
      window.is_signed_in = true;
      window.access_token = authResult.access_token;
      window.authResultexpiration =  authResult.expires_at;

      Auth.$scope.runTheProcess();
  }
  Auth.initSimple = function(){
      var timeNow = new Date().getTime()/1000;
      if (window.is_signed_in){


          var diff = window.authResultexpiration - timeNow;
          if (diff>0){
             Auth.$scope.immediateFailed = false;
             Auth.$scope.isSignedIn = true;
             Auth.$scope.runTheProcess();
          }
          else{

              // refresh token
              Auth.refreshToken();

          }
      // render Google+ sign-in
      }else{
            // check the gapi token
            var isGapiOk = Auth.checkGapiToken();
            if (isGapiOk){
                var gapiToken = gapi.auth.getToken();
                Auth.processAuth(gapiToken);
            }else{
              gapi.signin.render('myGsignin', {
                'callback': Auth.signIn,
                'clientid': '935370948155-a4ib9t8oijcekj8ck6dtdcidnfof4u8q.apps.googleusercontent.com',
                'scope': 'https://www.googleapis.com/auth/userinfo.email',
                'theme': 'dark',
                'cookiepolicy': 'single_host_origin',
                'accesstype': 'online',
                'width':'wide'
              });
            }
      }
  }
  Auth.init = function($scope){
      // make sure there is only one instance of Auth.init executed
      if (!window.countInitExec){
          window.countInitExec = 1;
      }else{
          window.countInitExec = window.countInitExec+1;
          var timeNow = new Date().getTime()/1000;
          Auth.$scope = $scope;
          if (typeof(Storage) != "undefined") {
              // Using the localStorage
              Auth.initWithLocalStorage();
          } else {
              // Using the window object
              Auth.initSimple();
          }
      }
  };
  Auth.signIn = function(authResult){
      // console.log('now signedin i will show you th new authResult');
      // console.log(authResult);
      localStorage.removeItem('access_token');
      if (authResult.status.google_logged_in){
        gapi.auth.setToken(authResult);
        Auth.processAuth(authResult);
      }else{
        window.location.replace('/sign-in');
      }

  };
  Auth.processAuth = function(authResult) {
      //Auth.$scope.immediateFailed = true;
      // console.log(authResult);
      window.isRefreshing = false;
      if (authResult) {
        if (authResult['access_token']){
          Auth.$scope.immediateFailed = false;
          Auth.$scope.isSignedIn = true;
          Auth.goAhead(authResult);
        }
        else{
          // Auth.renderForcedSignIn();
          window.location.replace('/sign-in');
        }

      } else {
            Auth.renderForcedSignIn();
      };

  };
  Auth.renderForcedSignIn = function(){
    window.authResult = null;
    Auth.$scope.immediateFailed = true;
    gapi.signin.render('myGsignin', {
      'callback': Auth.signIn,
      'clientid': '935370948155-a4ib9t8oijcekj8ck6dtdcidnfof4u8q.apps.googleusercontent.com',
      'scope': 'https://www.googleapis.com/auth/userinfo.email',
      'theme': 'dark',
      'cookiepolicy': 'single_host_origin',
      'accesstype': 'online',
      'width':'wide'
    });
  }
  Auth.refreshToken = function(){
    if (!window.isRefreshing){
        window.isRefreshing = true;
        Auth.$scope.$apply(); 
        Auth.renderForcedSignIn();
    }
    //window.location.reload(true);

  };

  return Auth;
});
{'fraction_non_alphanumeric': 0.13094883997620463, 'fraction_numerical': 0.011600237953599048, 'mean_word_length': 1.9249673771204872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 8, 'lorem ipsum': 0, 'www.': 8, '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': '3390168', 'n_tokens_mistral': 4014, 'n_tokens_neox': 3859, 'n_words': 650}