<commit_before>
(function(window, angular, undefined) {'use strict';

var oauthGoogleModule = angular.module('AngularOauthGoogle', [])

  .controller('oauthGoogleCtrl', function($scope) {

    $scope.account = '';

    var isReady = false;
    var isAuthenticated = false;

    initialize();

    function initialize () {
      var firstScriptTag = document.getElementsByTagName('script')[0];

      loadScript('http://www.google.com/jsapi');
      loadScript('https://apis.google.com/js/client.js?onload=onJSClientLoad');

      function loadScript (url) {
        var tag = document.createElement('script');
        tag.src = url;
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      }
    }

    window.onJSClientLoad = function () {
      isReady = true;
    };

    $scope.signin = function (clientId, clientScope, immediate) {
      if (!isReady || isAuthenticated) {
        return;
      }

      clientId = '@@google_crednetial';
      clientScope = [
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/youtube.readonly'
        // 'https://www.googleapis.com/auth/yt-analytics.readonly'
      ];

      gapi.auth.authorize({
        client_id: clientId,
        scope: clientScope,
        immediate: immediate || true
      }, handleAuthResult);
    };

    function handleAuthResult (authResult) {
      console.log(authResult);

      if (authResult) {
        console.log('Authenticated.');

        isAuthenticated = true;

        loadAPIClientInterfaces();
      } else {
        console.log('Failed.');
      }
    }

    function loadAPIClientInterfaces() {
      gapi.client.load('oauth2', 'v2', function() {
        var request = gapi.client.oauth2.userinfo.get();
        request.execute(function (res) {
          if (res.email) {
            $scope.account = res.email;
            $scope.$apply();
          }
        });
      });
    }

  });

})(window, window.angular);<commit_msg>Refactored code from controller to service<commit_after>
(function(window, angular, undefined) {'use strict';

angular.module('AngularOauthGoogle', [])
  .service('oauthGoogleService', function ($rootScope) {

    $rootScope.account = '';

    var isReady = false;
    var triedSignedIn = false;
    var isAuthenticated = false;

    var id = null;
    var scopes = null;

    initialize();

    function initialize () {
      var firstScriptTag = document.getElementsByTagName('script')[0];

      loadScript('http://www.google.com/jsapi');
      loadScript('https://apis.google.com/js/client.js?onload=onJSClientLoad');

      function loadScript (url) {
        var tag = document.createElement('script');
        tag.src = url;
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      }
    }

    window.onJSClientLoad = function () {
      isReady = true;

      if (triedSignedIn) {
        signIn();
      }
    };

    function signIn (immediate, clientId, clientScopes) {
      triedSignedIn = true;

      if (clientId) {
        id = clientId;
      }
      if (clientScopes) {
        scopes = clientScopes;
      }

      if (!isReady || isAuthenticated) {
        return;
      }

      gapi.auth.authorize({
        client_id: id,
        scope: scopes,
        immediate: immediate || true
      }, handleAuthResult);
    }

    function handleAuthResult (authResult) {
      console.log(authResult);

      if (authResult) {
        console.log('Authenticated.');

        isAuthenticated = true;

        loadAPIClientInterfaces();
      } else {
        console.log('Failed.');
      }
    }

    function loadAPIClientInterfaces() {
      gapi.client.load('oauth2', 'v2', function() {
        var request = gapi.client.oauth2.userinfo.get();
        request.execute(function (res) {
          if (res.email) {
            $rootScope.account = res.email;
            $rootScope.$apply();
          }
        });
      });
    }

    return {
      signIn: signIn
    };
  })

  .directive('oauthGoogle', function (oauthGoogleService) {
    return {
      restrict: 'E',
      link: function (scope, element, attrs) {
        oauthGoogleService.signIn(true, attrs.id, attrs.scopes.split(','));
      }
    };
  });

})(window, window.angular);
{'fraction_non_alphanumeric': 0.13708920187793427, 'fraction_numerical': 0.0018779342723004694, 'mean_word_length': 2.318535825545171, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, 'lorem ipsum': 0, 'www.': 5, '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': '4084312', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1275, 'n_words': 222}