-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (94 loc) · 2.93 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
;(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory)
} else if (typeof exports === 'object') {
module.exports = factory()
} else {
global.Index = factory()
}
}(this, function () {
var config = null
var directAccess = false
var gapiUrl = 'https://apis.google.com/js/api:client.js'
var gAuth = {
install: function (Vue, options) {
Vue.googleAuth = googleAuth
Vue.prototype.$googleAuth = googleAuth
if (typeof options === 'object') {
config = Object.assign(options, { scope: 'profile email https://www.googleapis.com/auth/plus.login' })
}
}
}
function googleAuth () {
return {
load: function (options) {
return new Promise(function (resolve, reject) {
if (window.gapi === undefined) {
installClient().then(function () {
return initClient(options)
}).then(function () {
resolve()
})
} else if (window.gapi !== undefined && window.gapi.auth2 === undefined) {
initClient(options).then(function () {
resolve()
})
} else {
resolve()
}
})
},
directAccess: function () {
directAccess = true
},
signIn: function (successCallback, errorCallback) {
if (directAccess) {
window.gapi.auth2.getAuthInstance().signIn().then(function (googleUser) {
successCallback(googleUser)
}, function (error) {
errorCallback(error)
})
} else {
window.gapi.auth2.getAuthInstance().grantOfflineAccess({'redirect_uri': 'postmessage'}).then(function (response) {
successCallback(response.code)
}, function (error) {
errorCallback(error)
})
}
},
signOut: function (successCallback, errorCallback) {
window.gapi.auth2.getAuthInstance().signOut().then(function () {
successCallback()
}, function (error) {
errorCallback(error)
})
}
}
}
function installClient () {
return new Promise(function (resolve, reject) {
var script = document.createElement('script')
script.src = gapiUrl
script.onreadystatechange = script.onload = function () {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
setTimeout(function () {
resolve()
}, 500)
}
}
document.getElementsByTagName('head')[0].appendChild(script)
})
}
function initClient (options) {
if (typeof options === 'object') {
config = Object.assign(options, { scope: 'profile email https://www.googleapis.com/auth/plus.login' })
}
return new Promise(function (resolve, reject) {
window.gapi.load('auth2', function () {
window.gapi.auth2.init(config)
resolve()
})
})
}
return gAuth
}))