-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaccount.js
More file actions
337 lines (279 loc) · 10.9 KB
/
account.js
File metadata and controls
337 lines (279 loc) · 10.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
function getURLParameter(name) {
return decodeURIComponent((new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(location.search) || [null, ""])[1].replace(/\+/g, "%20")) || null;
}
function dataURItoBlob(dataURI) {
// Convert base64 to raw binary data held in a string.
// Doesn't handle URLEncoded DataURIs.
var byteString = atob(dataURI.split(",")[1]);
// Separate out the mime component.
var mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0]
// Write the bytes of the string to an ArrayBuffer.
var ab = new ArrayBuffer(byteString.length);
// Create a view into the buffer.
var ia = new Uint8Array(ab);
// Set the bytes of the buffer to the correct values.
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// Write the ArrayBuffer to a blob, and you're done.
var blob = new Blob([ab], {type: mimeString});
return blob;
}
function change(user) {
if (typeof(Storage) !== "undefined") {
if (user && user.uid != null) {
// Sign in operation.
$("#signIn").css("display", "none");
$("#signUp").css("display", "none");
$(".signedIn").css("display", "block");
$(".notSignedIn").css("display", "none");
firebase.database().ref("users/" + user.uid + "/_settings/name").on("value", function(snapshot) {
$(".accountName").text(snapshot.val());
if (isStaff(currentUid)) {
$(".accountName").css("color", "#27ef70");
if (window.location.pathname.split("/").pop() == "account") {
$(".adminBanner").show();
}
} else if (isGameProxyPro(currentUid)) {
$(".accountName").css("color", "#b3c20f");
} else {
$(".accountName").css("color", "white");
if (window.location.pathname.split("/").pop() == "account") {
$(".adminBanner").hide();
}
}
setInterval(function() {
if (isStaff(currentUid)) {
$(".accountName").css("color", "#27ef70");
if (window.location.pathname.split("/").pop() == "account") {
$(".adminBanner").show();
}
} else if (isGameProxyPro(currentUid)) {
$(".accountName").css("color", "#b3c20f");
}
});
});
refreshPpic();
} else {
// Sign out operation.
$("#signIn").css("display", "block");
$("#signUp").css("display", "none");
$(".signedIn").css("display", "none");
$(".notSignedIn").css("display", "block");
}
} else {
alert("Sorry! You will not be able to use your GameProxy account on this device as it does not support HTML5 web storage.");
}
}
var currentUid = null;
var signingUp = false;
var ensureAuth = false;
if (getURLParameter("ensureAuth") == "true") {
firebase.auth().signOut();
ensureAuth = true;
$(function() {
$("#accountSignInMessage").text("You're going into some potentially dangerous account settings! Please sign in again for security purposes.")
});
}
if (getURLParameter("after") == "delete") {
$(function() {
$("#accountSignInMessage").text("Your account has been successfully deleted. We're sad to have you gone! 😔")
});
}
firebase.auth().onAuthStateChanged(function(user) {
if (user) {currentUid = user.uid;} else {currentUid = null;}
if (ensureAuth) {
ensureAuth = false;
return;
}
if (!signingUp) {
change(user);
}
if (user && !signingUp) {
if (getURLParameter("go") != null && window.location.pathname.split("/").pop() == "account" && user) {
window.location.replace(getURLParameter("go"));
}
}
});
function refreshPpic() {
try {
firebase.storage().ref("users/" + currentUid + "/_settings/ppic.png").getDownloadURL().then(function(data) {
$(".accountPicture").attr("src", data);
});
} catch (e) {
$(".accountPicture").attr("src", "/media/AnonUser.png");
}
}
function setPpic(data) {
var file = dataURItoBlob(data);
firebase.storage().ref("users/" + currentUid + "/_settings/ppic.png").put(file).then(function(snapshot) {
refreshPpic();
});
}
function setName(data) {
if (profanity.clean(data).trim() != "") {
firebase.database().ref("users/" + firebase.auth().currentUser.uid + "/_settings/name").set(profanity.clean(data).trim());
} else {
firebase.database().ref("users/" + firebase.auth().currentUser.uid + "/_settings/name").set("Anonymous");
}
}
function checkFields() {
if ($("#user").val() != "" && $("#pass").val() != "") {
return true;
} else {
document.getElementById("error").innerHTML = "Oops! You have not filled out all of the required fields.";
return false;
}
}
function checkUsername() {
if ($("#name").val() != "") {
return true;
} else {
document.getElementById("error").innerHTML = "Oops! You have not filled out all of the required fields.";
return false;
}
}
function signIn() {
document.getElementById("error").innerHTML = "";
if (checkFields()) {
firebase.auth().signInWithEmailAndPassword($("#user").val(), $("#pass").val()).catch(function(error) {
document.getElementById("error").innerHTML = "Oops! " + error.message;
});
}
}
function signOutBefore() {
document.getElementById("error").innerHTML = "";
if (checkFields()) {
$("#signIn").css("display", "none");
$("#signUp").css("display", "block");
}
}
function signUp() {
document.getElementById("error").innerHTML = "";
if (checkUsername()) {
signingUp = true;
firebase.auth().createUserWithEmailAndPassword($("#user").val(), $("#pass").val()).then(function() {
firebase.database().ref("users/" + firebase.auth().currentUser.uid + "/_settings/name").set(profanity.clean($("#name").val().substring(0, 20)).trim()).then(function() {
window.location.replace("/");
});
}).catch(function(error) {
document.getElementById("error").innerHTML = "Oops! " + error.message;
});
}
}
function signOut() {
$(".accountName").css("color", "white");
$(".adminBanner").hide();
document.getElementById("error").innerHTML = "";
firebase.auth().signOut().catch(function(error) {
document.getElementById("error").innerHTML = "Oops! Something went wrong on our side. Try again soon!";
});
}
function reset() {
$("#signIn").css("display", "block");
$("#signUp").css("display", "none");
}
function copyShareProfileLink() {
$(".shareProfileLink").removeAttr("disabled");
document.getElementsByClassName("shareProfileLink")[0].select();
document.execCommand("copy");
$(".shareProfileLink").attr("disabled", "");
}
function shareProfile() {
dialog(
"Share Profile",
`
<div class="center">
<p>Copy the link below to share your profile!</p>
<input class="shareProfileLink fullWidth" disabled>
<a href="javascript:copyShareProfileLink();">Just do it for me</a>
</div>
`
);
$(".shareProfileLink").val("https://gameproxy.host/profile?user=" + currentUid);
}
function changeEmailAddress() {
if ($("#accountNewEmail").val().trim() == $("#accountNewEmailAgain").val().trim()) {
firebase.auth().currentUser.updateEmail($("#accountNewEmail").val().trim()).then(function() {
alert("Your email address has been successfully changed! Use it to sign in from now on.", "Change successful");
}).catch(function(error) {
$("#emailError").text(error.message);
});
} else {
$("#emailError").text("Make sure that both email addresses match!");
}
}
function changePassword() {
if ($("#accountNewPassword").val().trim() == $("#accountNewPasswordAgain").val().trim()) {
firebase.auth().currentUser.updatePassword($("#accountNewPassword").val().trim()).then(function() {
alert("Your password has been successfully changed! Use it to sign in from now on.", "Change successful");
}).catch(function(error) {
$("#passwordError").text(error.message);
});
} else {
$("#passwordError").text("Make sure that both passwords match!");
}
}
function deleteAccountError(error) {
alert(`
Sorry! Your account couldn't be deleted. Please try signing out or
signing back in again. If that doesn't work, please contact our support
team who will be happy to do it manually:
<a href="mailto:support@gameproxy.host">support@gameproxy.host</a>
`, "Couldn't delete account");
console.log("{gpxy:debug} Account deletion error:", error);
}
function deleteAccount() {
firebase.storage().ref("users/" + currentUid + "/_settings/ppic.png").delete().then(function() {
firebase.database().ref("users/" + firebase.auth().currentUser.uid + "/_settings/name").set("[Deleted Account]").then(function() {
firebase.auth().currentUser.delete().then(function() {
window.location.replace("account?after=delete");
}).catch(function(error) {
deleteAccountError(error);
});
}).catch(function(error) {
deleteAccountError(error);
});
}).catch(function() {
firebase.auth().currentUser.delete().then(function() {
window.location.replace("account?after=delete");
}).catch(function(error) {
deleteAccountError(error);
});
});
}
function showDeleteAccountDialog() {
dialog("Delete account?", `
Do you really want to delete your account? This is your final chance to cancel!
`, [
{text: "Cancel", onclick: "closeDialog();", type: "bad"},
{text: "Delete", onclick: "deleteAccount(); closeDialog();", type: "reallyBad"}
]);
}
var input = document.getElementById("pass");
try {
input.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
signIn();
}
});
} catch (e) {}
$("#ppicFile").on("change", function(evt) {
function handleFile(file) {
var fileData = new FileReader();
fileData.readAsDataURL(file);
fileData.onload = function(evt) {
setPpic(evt.target.result);
}
}
var files = evt.target.files;
handleFile(files[0]);
});
$(function() {
setInterval(function() {
if (currentUid != null) {
firebase.database().ref("users/" + currentUid + "/_settings/lastSeen").set(firebase.database.ServerValue.TIMESTAMP);
}
}, 5000);
});