Skip to content

Instantly share code, notes, and snippets.

@jrcahoon
Forked from jakemmarsh/controllers.js
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrcahoon/037c3690e3430cfadff2 to your computer and use it in GitHub Desktop.
Save jrcahoon/037c3690e3430cfadff2 to your computer and use it in GitHub Desktop.
define(['angular', 'services'], function (angular) {
'use strict';
return angular.module('myApp.controllers', ['myApp.services'])
.controller('IndexCtrl', ['$scope', 'googleService', function ($scope, googleService) {
$scope.login = function () {
googleService.login().then(function (data) {
// do something with returned data
console.log(data.email);
}, function (err) {
console.log('Failed: ' + err);
});
};
}]);
});
define(['angular'], function (angular) {
'use strict';
angular.module('myApp.services', [])
.service('googleService', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) {
var clientId = '{CLIENT_ID}',
apiKey = '{API_KEY}',
scopes = '{SCOPES}',
domain = '{OPTIONAL DOMAIN}',
deferred = $q.defer();
this.login = function () {
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: false,
hd: domain
}, this.handleAuthResult);
return deferred.promise;
}
this.handleClientLoad = function () {
gapi.client.setApiKey(apiKey);
gapi.auth.init(function () { });
window.setTimeout(checkAuth, 1);
};
this.checkAuth = function() {
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: true,
hd: domain
}, this.handleAuthResult);
};
this.handleAuthResult = function(authResult) {
if (authResult && !authResult.error) {
var data = {};
gapi.client.load('oauth2', 'v2', function () {
var request = gapi.client.oauth2.userinfo.get();
request.execute(function (resp) {
data.email = resp.email;
});
});
deferred.resolve(data);
} else {
deferred.reject('error');
}
};
this.handleAuthClick = function(event) {
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: false,
hd: domain
}, this.handleAuthResult);
return false;
};
}]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment