/
google-plus-signin.js
56 lines (47 loc) · 1.89 KB
/
google-plus-signin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
/*
* angular-google-plus-directive v0.0.1
* ♡ CopyHeart 2013 by Jerad Bitner http://jeradbitner.com
* Copying is an act of love. Please copy.
*/
angular.module('directive.g+signin', []).
directive('googlePlusSignin', function () {
var ending = /\.apps\.googleusercontent\.com$/;
return {
restrict: 'E',
template: '<span class="g-signin"></span>',
replace: true,
link: function (scope, element, attrs) {
attrs.clientid += (ending.test(attrs.clientid) ? '' : '.apps.googleusercontent.com');
attrs.$set('data-clientid', attrs.clientid);
// Some default values, based on prior versions of this directive
var defaults = {
callback: 'signinCallback',
cookiepolicy: 'single_host_origin',
requestvisibleactions: 'http://schemas.google.com/AddActivity',
scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
width: 'wide'
};
// Provide default values if not explicitly set
angular.forEach(Object.getOwnPropertyNames(defaults), function(propName) {
if (!attrs.hasOwnProperty(propName)) {
attrs.$set('data-' + propName, defaults[propName]);
}
});
// Asynchronously load the G+ SDK.
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/client:plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
}
};
}).run(['$window','$rootScope',function($window, $rootScope) {
$window.signinCallback = function (authResult) {
if (authResult && authResult.access_token){
$rootScope.$broadcast('event:google-plus-signin-success', authResult);
} else {
$rootScope.$broadcast('event:google-plus-signin-failure', authResult);
}
};
}]);