-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathloginsignup.js
356 lines (330 loc) · 14.8 KB
/
loginsignup.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
function createordestroybutton(win) {
if (sessionStorage.getItem('token') != null) {
var navmenu = document.getElementById("navmenu");
navmenu.removeChild(navmenu.lastChild);
var btn = document.createElement("button");
btn.type = "button";
btn.className = "btn btn-outline-primary";
btn.setAttribute("onclick", "logout(\'" + win + "'\)");
btn.innerHTML = "Logout";
navmenu.appendChild(btn);
} else {
var navmenu = document.getElementById("navmenu");
navmenu.removeChild(navmenu.lastChild);
var btn = document.createElement("button");
btn.type = "button";
btn.className = "btn btn-outline-primary";
btn.setAttribute("data-toggle", "modal");
btn.setAttribute("data-target", "#loginpopup");
btn.innerHTML = "Login";
navmenu.appendChild(btn);
}
}
function dashboardandgallerybutton() {
var token = sessionStorage.getItem('token');
var el = document.getElementById('top');
var eluser = document.querySelector('#navmenu .navbar-nav');
if (document.getElementById('btns') != undefined) {
el.removeChild(document.getElementById('btns'));
}
if (document.getElementById('user') != undefined) {
eluser.removeChild(document.getElementById('user'));
}
var div = document.createElement('div');
div.id = 'btns';
if (token != null) {
var tempuserinfo = '<div id="user" onclick="$(\'#userinfo\').modal(\'show\')" style="cursor: pointer;display: inline"><img style="height: 30px;" src="svglogos/user.svg" alt="user">\n' +
' </div><div class="dropdown-divider"></div>';
eluser.innerHTML += tempuserinfo;
var tempdashboardgallery = '<div class="text-center row">\n' +
' <div class="col-6">\n' +
' <button type="button" onclick="window.open(\'dashboard.html\',\'_self\')" class="btn btn-primary"\n' +
' style="width: 150px;">View Dashboard\n' +
' </button>\n' +
' </div>\n' +
' <div class="col-6">\n' +
' <button type="button" onclick="opengallery();" class="btn btn-outline-primary" style="width: 150px">View Gallery</button>\n' +
' </div>\n' +
' </div>'
div.innerHTML = tempdashboardgallery;
} else {
var tempdashboardgallery = '<div class="row" style="padding-top: 10px;">\n' +
' <div class="col-12">\n' +
' <button type="button" onclick="$(\'#loginpopup\').modal(\'show\');" class="btn btn-outline-primary getstartedbutton">Get Started</button>\n' +
' </div>\n' +
' </div>'
div.innerHTML = tempdashboardgallery;
}
el.appendChild(div);
}
function checkloginindashboard() {
if (sessionStorage.getItem('token') == null) {
$('#loginpopup').modal('show');
}
}
function signup() {
var name = document.getElementById("name").value;
var email = document.getElementById("emailnew").value;
var password = document.getElementById("passwordnew").value;
var organization = document.getElementById('organization').value;
var role = document.getElementById('role').value;
loader('show');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 201) {
loader('hide');
alertpopup('Account Created Successfully!', 'open', '#28a745');
setTimeout(function () {
$('#signuppopup').modal('hide');
$('#loginpopup').modal('show');
document.getElementById('email').value = email;
document.getElementById('password').value = '';
}, 2000);
} else if (this.readyState == 4 && this.status == 409) {
loader('hide');
alertpopup('Account Already Exists\nfor this Email', 'open', '#dc3545');
} else if (this.readyState == 4 && this.status == 500) {
loader('hide');
alertpopup('Failed to Create Account\nInternal Server Error', 'open', '#dc3545');
}
};
xhttp.open("POST", "https://api.iwasat.events/api/v1/register", true);
xhttp.onerror = function () {
loader('hide');
alertpopup("Unexpected Error!", 'open', '#dc3545');
};
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify({email: email, name: name, organisation: organization, password: password, role: role}));
}
function userinfodisplay() {
var el = document.getElementById('bodyuserinfo');
var userinfo = JSON.parse(sessionStorage.getItem('userinfo'));
var temp = '<table class="table">'
for (i in userinfo) {
temp += '<tr>';
temp += '<td>' + i + '</td>';
temp += '<td>' + userinfo[i] + '</td>';
temp += '</tr>'
}
temp += '</div>'
el.innerHTML = temp;
}
function login(win) {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
loader('show');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 202) {
loader('hide');
var userinfo = {
"Name": JSON.parse(this.responseText)["name"],
"E-mail": JSON.parse(this.responseText)["email"],
"Organisation": JSON.parse(this.responseText)["organisation"],
"Role": JSON.parse(this.responseText)["role"]
};
sessionStorage.setItem('userinfo', JSON.stringify(userinfo));
sessionStorage.setItem('token', JSON.parse(this.responseText)["token"]);
createordestroybutton(win);
userinfodisplay();
if (win == 'gallery') {
getframes();
}
setTimeout(function () {
if (win == 'index') {
dashboardandgallerybutton();
window.open('dashboard.html', '_self');
}
}, 1000);
$('#loginpopup').modal('hide');
} else if (this.readyState == 4 && this.status == 401) {
loader('hide');
alertpopup('Invalid Email or Password', 'open', '#dc3545');
}
};
xhttp.open("POST", "https://api.iwasat.events/api/v1/login", true);
xhttp.onerror = function () {
loader('hide');
alertpopup("Unexpected Error!", 'open', '#dc3545');
};
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify({email: email, password: password}));
}
function deleteframe(frameid, frameindex) {
if (confirm("Delete this Frame?")) {
loader('show');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 201) {
loader('hide');
//delete from array
if (frameindex > -1) {
arrayofframes.splice(frameindex, 1);
}
//delete from array
displayarrayofframes(); //now display
}
};
xhttp.open("DELETE", "https://api.iwasat.events/api/v1/frames?id=" + frameid, true);
xhttp.onerror = function () {
loader('hide');
alertpopup("Unexpected Error!", 'open', '#dc3545');
};
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Authorization", `Bearer ${sessionStorage.getItem('token')}`);
xhttp.send();
}
}
function displayarrayofframes() {
if (arrayofframes.length === 0) {
var temp = '<div class="text-center">\n' +
' <div>It Seems No Saved Frames Yet!</div>\n' +
' <span class="btn btn-outline-primary" onclick="window.open(\'dashboard.html\',\'_self\')"><img src="svglogos/add.svg" style="height: 20px;padding-right: 10px;padding-bottom: 3px;" alt="add">New Badge</span>\n' +
' </div>';
document.querySelector(".container").innerHTML = temp;
} else {
var temp = "";
for (var i = 0; i < arrayofframes.length; i++) {
temp += '<div class="col-sm-4">'
temp += '<img class="displayframeimage" src="' + arrayofframes[i]["frame_data"] + '">'
temp += '<div class="overlay">'
temp += '<div class="text">' +
'<ul class="list">' +
'<li onclick="download(\'' + arrayofframes[i]["frame_data"] + '\');"><span><img src="svglogos/download.svg" class="iconsimagehover">Download</span></li>' +
'<li onclick="deleteframe(\'' + arrayofframes[i]["frame_id"] + '\',\'' + i + '\');"><span><img src="svglogos/delete.svg" class="iconsimagehover">Delete </span></li>' +
'</ul>' +
'</div>'
temp += '</div>'
temp += '</div>'
}
document.getElementById('main').innerHTML = temp;
}
}
var arrayofframes;
function getframes() {
var token = sessionStorage.getItem('token');
loader('show');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 201) {
arrayofframes = JSON.parse(this.responseText)["frames"];
displayarrayofframes();
loader('hide');
} else if (this.readyState == 4 && this.status == 401) {
loader('hide');
alertpopup(this.responseText, 'open', '#dc3545');
}
};
xhttp.open("GET", "https://api.iwasat.events/api/v1/frames", true);
xhttp.onerror = function () {
loader('hide');
alertpopup("Unexpected Error!", 'open', '#dc3545');
};
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.setRequestHeader("Authorization", `Bearer ${sessionStorage.getItem('token')}`);
xhttp.send();
}
function saveframe() {
var token = sessionStorage.getItem('token');
if (token == null) {
$('#loginpopup').modal('show');
} else {
var dataurl = canvas.toDataURL("image/png;base64");
var xhttp = new XMLHttpRequest();
loader('show');
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 201) {
loader('hide');
alertpopup('Added Successfully', 'open', '#28a745');
} else if (this.readyState == 4 && this.status == 400) {
loader('hide');
alertpopup('Error While Saving\nTry Again', 'open', '#dc3545');
} else if (this.readyState == 4 && this.status == 401) {
loader('hide');
alertpopup('Error While Saving\nLogin Again', 'open', '#dc3545');
}
};
xhttp.open("POST", "https://api.iwasat.events/api/v1/frames");
xhttp.onerror = function () {
loader('hide');
alertpopup("Unexpected Error!", 'open', '#dc3545');
};
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.setRequestHeader("Authorization", `Bearer ${sessionStorage.getItem("token")}`);
xhttp.send(JSON.stringify({frame: dataurl}));
}
}
function resetpassword() {
var email = document.getElementById('email').value;
loader('show');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
loader('hide');
alertpopup('Reset Link Sent to your E-mail', 'open', '#28a745');
} else if (this.readyState == 4 && this.status == 400) {
loader('hide');
alertpopup('Enter E-mail', 'open', '#dc3545');
} else if (this.readyState == 4 && this.status == 401) {
loader('hide');
alertpopup('Seems there\'s no Account Registered with this E-mail', 'open', '#dc3545');
} else if (this.readyState == 4 && this.status == 500) {
loader('hide');
alertpopup('Internal Server Error!', 'open', '#dc3545');
}
};
xhttp.open("POST", "https://api.iwasat.events/api/v1/send-reset-mail", true);
xhttp.onerror = function () {
loader('hide');
alertpopup("Unexpected Error!", 'open', '#dc3545');
};
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify({email: email}));
}
function updatepassword() {
var password = document.getElementById('password').value;
const params = new URLSearchParams(window.location.search)
var resettoken = params.get('token');
loader('show');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
loader('hide');
alertpopup('Password Changed Successfully', 'open', '#28a745');
setTimeout(function () {
window.open('dashboard.html', '_self');
}, 2000);
} else if (this.readyState == 4 && this.status == 400) {
loader('hide');
alertpopup('Token or Password is absent in the request body', 'open', '#dc3545');
} else if (this.readyState == 4 && this.status == 401) {
loader('hide');
alertpopup('Invalid Token!', 'open', '#dc3545');
} else if (this.readyState == 4 && this.status == 500) {
loader('hide');
alertpopup('Internal Server Error!', 'open', '#dc3545');
}
};
xhttp.open("POST", "https://api.iwasat.events/api/v1/update-password", true);
xhttp.onerror = function () {
loader('hide');
alertpopup("Unexpected Error!", 'open', '#dc3545');
};
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify({password: password, token: resettoken}));
}
function logout(win) {
sessionStorage.removeItem('token');
sessionStorage.removeItem('userinfo');
createordestroybutton('index');
createordestroybutton('dashboard');
if (win == 'gallery') {
$('#loginpopup').modal('show');
} else if (win == 'index') {
dashboardandgallerybutton();
} else if (win == 'dashboard') {
checkloginindashboard();
}
}