-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
85 lines (80 loc) · 2.81 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
import { gapi, gapiComplete } from './gapiScript';
/**
* Function to load gapi auth2 from a gapi that you provied
* Check full docs here: https://developers.google.com/identity/sign-in/web/reference#auth_setup
* @param {Object} gapiScript gapi script object
* @param {string} clientId Your google clientID string
* @param {Array.<string[]>} scopes The scopes to request, as a space-delimited string. Optional if fetch_basic_profile is not set to false. Check possible scopes on google docs: https://developers.google.com/identity/protocols/oauth2/scopes
*/
const loadAuth2 = async function (gapiScript, clientId, scopes) {
return new Promise(resolve => {
gapiScript.load('auth2', () => {
resolve(gapiScript.auth2.init({
client_id: clientId,
scope: scopes
}));
});
});
}
/**
* Function to init gapi auth2 with props
* @param {Object} gapiScript gapi script object
* @param {*} props Possible props to init gapi auth2, check the options on google docs: https://developers.google.com/identity/sign-in/web/reference#gapiauth2clientconfig
*/
const loadAuth2WithProps = async function (gapiScript, props) {
return new Promise(resolve => {
gapiScript.load('auth2', () => {
resolve(gapiScript.auth2.init(props));
});
});
}
/**
*
* @param {Object} gapiScript gapi script object
* @param {string} clientId Your google clientID string
* @param {Array.<string[]>} scopes The scopes to request, as a space-delimited string. Optional if fetch_basic_profile is not set to false. Check possible scopes on google docs: https://developers.google.com/identity/protocols/oauth2/scopes
*/
const loadClientAuth2 = async function (gapiScript, clientId, scopes) {
return new Promise(resolve => {
gapiScript.load('client', () => {
resolve(gapiScript.client.init({
client_id: clientId,
scope: scopes
}));
});
gapiScript.load('auth2', () => {
resolve(gapiScript.client.init({
client_id: clientId,
scope: scopes
}));
});
});
}
/**
* A very special function to load the gapi inside the DOM, directly.
* So it'll load the real and most recent gapi-scrip from google and attach to DOM:
* let gapi = loadGapiInsideDOM();
* Now you can use it anywhere.
*/
const loadGapiInsideDOM = async function () {
return new Promise(resolve => {
const element = document.getElementsByTagName('script')[0];
const js = document.createElement('script');
js.id = 'google-platform';
js.src = '//apis.google.com/js/platform.js';
js.async = true;
js.defer = true;
element.parentNode.insertBefore(js, element);
js.onload = async () => {
resolve(window.gapi);
}
});
}
export {
gapi,
gapiComplete,
loadAuth2,
loadAuth2WithProps,
loadClientAuth2,
loadGapiInsideDOM,
};