This repository was archived by the owner on Apr 5, 2022. It is now read-only.
forked from cs4241-20a/a3-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (64 loc) · 1.93 KB
/
script.js
File metadata and controls
79 lines (64 loc) · 1.93 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
var users = [];
//var meetings = [];
var account = "guest"
//const meetingList = document.getElementById("meetings");
const login = document.querySelector("form");
const error = document.getElementById("error");
const github = document.getElementById("github");
function addUser(user, pass, id) {
const newUser = document.createElement("li");
newUser.innerText = user + " " + pass;
users.push({
"user": user,
"pass": pass,
"id": id
});
}
login.addEventListener("submit", event => {
event.preventDefault();
let newUser = login.elements.username.value;
let newPass = login.elements.password.value;
let createAccount = true;
users.forEach(user => {
if(user.user.localeCompare(newUser) === 0) {
createAccount = false;
if(user.pass.localeCompare(newPass) === 0) {
account = newUser;
localStorage.setItem("account", newUser);
document.location.href = "/schedule.html";
}
else {
error.innerHTML = "<span class='label label-danger'>ERROR: Wrong Password for this Username</span>";
}
}
})
if(createAccount) {
fetch("/add", {
method: "POST",
body: JSON.stringify({ user: newUser, pass: newPass }),
headers: {
"Content-Type": "application/json"
}
})
.then(res => res.json())
.then(json => {
addUser(json.user, json.pass, json._id);
account = json.user;
localStorage.setItem("account", newUser);
document.location.href = "/schedule.html";
});
}
login.reset();
login.elements.username.focus();
});
github.onclick = function() {
localStorage.setItem("account", "github");
document.location.href = `https://github.com/login/oauth/authorize?client_id=bbe908208af155698563`;
}
window.onload = function() {
fetch("/users")
.then(res => res.json())
.then(json => {
Array.from(json).forEach(user => addUser(user.user, user.pass, user._id))
})
}