This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_auth.js
143 lines (122 loc) · 3.36 KB
/
google_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
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
let signin_button = document.getElementById("signin_button")
let signout_button = document.getElementById("signout_button")
let user_name = document.getElementById("user_name")
let user_image = document.getElementById("user_image")
let auth_divs = {
false: document.getElementById("auth_false"),
true: document.getElementById("auth_true"),
}
var GoogleAuth
function initGapi() {
gapi.load('client', initGapiClient)
}
function initGapiClient() {
gapi.client.init({
'apiKey': 'AIzaSyAmOXAXhaj_qNqlyyXoWVPXK_E7mmKdBvU',
'clientId': '917679730606-hhojroi8m0q1u2tjaa6pppf0r54o32hb.apps.googleusercontent.com',
'scope': 'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata',
'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest']
}).then(function () {
GoogleAuth = gapi.auth2.getAuthInstance()
GoogleAuth.isSignedIn.listen(updateSigninStatus)
updateSigninStatus(GoogleAuth.isSignedIn.get())
})
}
function updateFile(fileId, fileMetadata, fileData, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(fileMetadata) +
delimiter +
'Content-Type: application/json\r\n' +
'\r\n' +
JSON.stringify(fileData) +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files/' + fileId,
'method': 'PUT',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody})
if (!callback) {
callback = function(file) {}
}
request.execute(callback);
}
let saveFileId
function clearAppDataFolder() {
gapi.client.drive.files.list(
{spaces: 'appDataFolder'}
).then(function(response) {
for (const file of response.result.files) {
gapi.client.drive.files.delete({fileId: file.id}).then()
}
})
}
function load() {
gapi.client.drive.files.list({
spaces: 'appDataFolder'
}).then(function(response) {
for (const file of response.result.files) {
if (file.name == 'overclicker.save') {
saveFileId = file.id
break;
}
}
if (saveFileId) {
gapi.client.drive.files.get({
fileId: saveFileId,
alt: "media"
}).then(function(response) {
if (response.result !== false) {
gameInfo = response.result
updateValue(0)
} else {
resetGame()
}
})
}
})
}
function save() {
if (saveFileId) {
updateFile(saveFileId, {}, gameInfo)
} else {
gapi.client.drive.files.create({
resource: {
'name': 'overclicker.save',
'mimeType': 'application/json',
'parents': ['appDataFolder'],
},
}).then(function(response) {
saveFileId = response.result.id
})
updateFile(saveFileId, {}, gameInfo)
}
}
signin_button.addEventListener("click", function() {
GoogleAuth.signIn()
})
signout_button.addEventListener("click", function() {
save()
GoogleAuth.signOut()
})
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
let user = GoogleAuth.currentUser.get()
let profile = user.getBasicProfile()
user_name.innerText = profile.getName()
user_image.src = profile.getImageUrl()
load()
} else {
saveFileId = undefined
resetGame()
}
auth_divs[isSignedIn].style.display = "block"
auth_divs[!isSignedIn].style.display = "none"
}