-
Notifications
You must be signed in to change notification settings - Fork 35
/
auth.js
68 lines (55 loc) · 1.67 KB
/
auth.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
import jwtHelper from './helpers/jwt-helper.js';
const state = {
credential: null,
access_token: null
};
async function loadAccessToken(gapi_client_id, gapi_scopes) {
return new Promise((resolve, reject) => {
const tokenClient = google.accounts.oauth2.initTokenClient({
client_id: gapi_client_id,
scope: gapi_scopes,
prompt: '',
callback: (resp) => {
if (resp.error !== undefined) {
reject(resp);
}
resolve(resp.access_token);
}
});
// GSI TokenClient.requestAccessToken() method displays a popup when invoked,
// the initTokenClient callback (above) will be called once process is complete.
tokenClient.requestAccessToken();
});
}
export default {
async init(gapi_client_id, gapi_scopes, signinCallback) {
await global_gisLoadPromise;
google.accounts.id.initialize({
client_id: gapi_client_id,
callback: async (response) => {
try {
state.credential = jwtHelper.parseJwt(response.credential);
state.access_token = await loadAccessToken(gapi_client_id, gapi_scopes);
signinCallback(state.credential.name);
}
catch (err) {
signinCallback('ERROR: ' + JSON.stringify(err));
}
}
});
},
getCachedAccessToken() {
return state.access_token;
},
renderButton(buttonContainerId) {
// https://developers.google.com/identity/gsi/web/guides/display-button#javascript
google.accounts.id.renderButton(
document.getElementById(buttonContainerId),
{ theme: "outline", size: "large" }
);
google.accounts.id.prompt();
},
reset() {
google.accounts.oauth2.revoke();
}
}