-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.ts
181 lines (168 loc) · 4.97 KB
/
index.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import _Vue from 'vue';
declare global {
interface Window { gapi: any; }
}
const googleAuth = ((): any => {
const installClient = () => {
const apiUrl = 'https://apis.google.com/js/api.js';
return new Promise<void>((resolve) => {
const script: any = document.createElement('script');
script.src = apiUrl;
script.onreadystatechange = script.onload = () => {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
setTimeout(() => {
resolve();
}, 500);
}
};
document.getElementsByTagName('head')[0].appendChild(script);
});
};
const initClient = (config: any) => {
return new Promise((resolve, reject) => {
window.gapi.load('auth2', () => {
window.gapi.auth2.init(config)
.then(() => {
resolve(window.gapi);
}).catch((error: any) => {
reject(error);
});
});
});
};
const Auth = function (this: any) {
this.GoogleAuth = null; /* window.gapi.auth2.getAuthInstance() */
this.isAuthorized = false;
this.isInit = false;
this.prompt = null;
this.isLoaded = () => {
// tslint:disable-next-line
console.warn('isLoaded() will be deprecated. You can use "this.$gAuth.isInit"');
return !!this.GoogleAuth;
};
this.load = (config: any, prompt: string) => {
installClient()
.then(() => {
return initClient(config);
})
.then((gapi: any) => {
this.GoogleAuth = gapi.auth2.getAuthInstance();
this.isInit = true;
this.prompt = prompt;
this.isAuthorized = this.GoogleAuth.isSignedIn.get();
}).catch((error) => {
console.error(error);
});
};
this.signIn = (successCallback: any, errorCallback: any) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') {
errorCallback(false);
}
reject(false);
return;
}
this.GoogleAuth.signIn()
.then((googleUser: any) => {
if (typeof successCallback === 'function') {
successCallback(googleUser);
}
this.isAuthorized = this.GoogleAuth.isSignedIn.get();
resolve(googleUser);
})
.catch((error: any) => {
if (typeof errorCallback === 'function') {
errorCallback(error);
}
reject(error);
});
});
};
this.getAuthCode = (successCallback: any, errorCallback: any) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') {
errorCallback(false);
}
reject(false);
return;
}
this.GoogleAuth.grantOfflineAccess({ prompt: this.prompt })
.then((resp: any) => {
if (typeof successCallback === 'function') {
successCallback(resp.code);
}
resolve(resp.code);
})
.catch((error: any) => {
if (typeof errorCallback === 'function') {
errorCallback(error);
}
reject(error);
});
});
};
this.signOut = (successCallback: any, errorCallback: any) => {
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: any) => {
if (typeof errorCallback === 'function') {
errorCallback(error);
}
reject(error);
});
});
};
};
return new (Auth as any);
})();
function installGoogleAuthPlugin(Vue: typeof _Vue, options?: any): void {
// set config
let GoogleAuthConfig: any = null;
const GoogleAuthDefaultConfig = {
scope: 'profile email',
};
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) {
// tslint:disable-next-line
console.warn('clientId is required');
}
} else {
// tslint:disable-next-line
console.warn('invalid option type. Object type accepted only');
}
// Install Vue plugin
Object.defineProperties(Vue.prototype, {
$gAuth: {
get: () => {
return googleAuth;
},
},
});
googleAuth.load(GoogleAuthConfig, prompt);
}
// tslint:disable-next-line
export default installGoogleAuthPlugin;