This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.js
74 lines (63 loc) · 2.11 KB
/
service.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* global angular */
angular.module('dinoboff.gapi', []).factory('dinoGapiClientLoader', function ($window, $q, $rootScope) {
'use strict';
return function(apiDetails) {
var q = $q.defer();
$window.gapi.load('auth:client', function () {
if (!apiDetails || !apiDetails.name) {
q.resolve($window.gapi);
$rootScope.$apply();
return;
}
$window.gapi.client.load(
apiDetails.name,
apiDetails.version,
function () {
q.resolve($window.gapi);
$rootScope.$apply();
},
apiDetails.root
);
});
return q.promise;
};
}).factory('dinoGapiOAuth2', function ($rootScope, $q, dinoGapiClientLoader) {
'use strict';
var api = {
gapi: null,
accessToken: null,
idToken: null,
config: {scopes: '', clientId: ''},
loader: dinoGapiClientLoader,
login: function (mode) {
/* jshint camelcase: false */
var q = $q.defer();
api.gapi.auth.authorize({
client_id: api.config.clientId,
scope: api.config.scopes,
immediate: mode,
response_type: 'token id_token'
}, api.userAuthed.bind(q));
return q.promise;
},
userAuthed: function () {
api.gapi.client.oauth2.userinfo.get().execute(api._getUserInfo.bind(this));
},
_getUserInfo: function (resp) {
/* jshint camelcase: false */
if (!resp.code) {
api.accessToken = api.gapi.auth.getToken();
api.idToken = api.accessToken.id_token;
api.gapi.auth.setToken(api.idToken);
this.resolve(resp.result);
} else {
this.reject(resp.message);
}
$rootScope.$apply();
}
};
return api.loader({name: 'oauth2', version: 'v2'}).then(function (gapi) {
api.gapi = gapi;
return api;
});
});