forked from guruahn/vue-google-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (144 loc) · 5.43 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var googleAuth = (function () {
function installClient() {
var apiUrl = 'https://apis.google.com/js/api.js'
return new Promise((resolve) => {
var script = document.createElement('script')
script.src = apiUrl
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(config) {
return new Promise((resolve) => {
window.gapi.load('auth2', () => {
window.gapi.auth2.init(config)
.then(() => {
resolve(window.gapi)
})
})
})
}
function Auth() {
if (!(this instanceof Auth))
return new Auth()
this.GoogleAuth = null /* window.gapi.auth2.getAuthInstance() */
this.isAuthorized = false
this.isInit = false
this.prompt = null
this.config = {}
this.isLoaded = function () {
/* eslint-disable */
console.warn('isLoaded() will be deprecated. You can use "this.$gAuth.isInit"')
return !!this.GoogleAuth
};
this.init = (config, prompt) => {
this.prompt = prompt
this.config = config
if(config.autoload) {
this.load()
}
}
this.load = () => {
installClient()
.then(() => {
return initClient(this.config)
})
.then((gapi) => {
this.GoogleAuth = gapi.auth2.getAuthInstance()
this.isInit = true
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
})
};
this.signIn = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signIn()
.then(googleUser => {
if (typeof successCallback === 'function') successCallback(googleUser)
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
resolve(googleUser)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};
this.getAuthCode = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.grantOfflineAccess({ prompt: this.prompt })
.then(function (resp) {
if (typeof successCallback === 'function') successCallback(resp.code)
resolve(resp.code)
})
.catch(function (error) {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};
this.signOut = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signOut()
.then(() => {
if (typeof successCallback === 'function') successCallback()
this.isAuthorized = false
resolve(true)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};
}
return new Auth()
})();
function installGoogleAuthPlugin(Vue, options) {
/* eslint-disable */
//set config
let GoogleAuthConfig = null
let GoogleAuthDefaultConfig = { scope: 'profile email', discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'], autoload: true }
let prompt = 'select_account'
if (typeof options === 'object') {
GoogleAuthConfig = Object.assign(GoogleAuthDefaultConfig, options)
if (options.scope) GoogleAuthConfig.scope = options.scope
if (options.prompt) prompt = options.prompt
if (!options.clientId) {
console.warn('clientId is required')
}
} else {
console.warn('invalid option type. Object type accepted only')
}
//Install Vue plugin
Vue.gAuth = googleAuth
Object.defineProperties(Vue.prototype, {
$gAuth: {
get: function () {
return Vue.gAuth
}
}
})
Vue.gAuth.init(GoogleAuthConfig, prompt)
}
export default installGoogleAuthPlugin