-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
250 lines (199 loc) · 8.36 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
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
"use strict";
// Defining Firebase authentication elements
const txtEmail = document.getElementById('username_login');
const txtPassword = document.getElementById('password_login');
const txtEmail_Reg = document.getElementById('username_reg');
const txtPassword_Reg = document.getElementById('password_reg');
const btnLogin = document.getElementById('btnLogin');
const btnRegister =document.getElementById('btnRegister');
const btnLogout = document.getElementById('btnLogout');
const btnLoginheader = document.getElementById('loginnavbutton');
const btnRegisterheader = document.getElementById('regnavbutton');
const btnCreatePost = document.getElementById('createnewpostmodal');
const btnMyPosts = document.getElementById('mypostsbtn');
// Authenticate user registration
btnRegister.addEventListener('click', e => {
const email = txtEmail_Reg.value;
const pass = txtPassword_Reg.value;
const promise = auth.createUserWithEmailAndPassword(email,pass);
promise.catch(e => console.log(e.message));
});
// Authenticate user login
btnLogin.addEventListener('click', e => {
const email = txtEmail.value;
const pass = txtPassword.value;
const promise = auth.signInWithEmailAndPassword(email,pass);
promise.catch(e => console.log(e.message));
});
// Logout current user
btnLogout.addEventListener('click', e => {
firebase.auth().signOut();
});
// Realtime listener for authentication state change (logged in/logged out)
auth.onAuthStateChanged(firebaseUser => {
if(firebaseUser){
console.log(firebaseUser);
btnLogout.classList.remove('d-none');
btnMyPosts.classList.remove('d-none');
btnLoginheader.classList.add('d-none');
btnRegisterheader.classList.add('d-none');
//Prevent users who are not logged in from initating new post entry
btnCreatePost.classList.remove('d-none');
} else {
console.log('Not logged in.');
btnLogout.classList.add('d-none');
}
});
// Post user registration (authentication granted via Firebase API upon submission)
document.getElementById('btnRegister').addEventListener('click', async function(event){
event.preventDefault();
try{
let data = {
"fname": document.getElementById('first_reg').value,
"lname" : document. getElementById('last_reg').value,
"username" : document.getElementById('username_reg').value,
"password": document.getElementById('password_reg').value,
"confirmpass" : document.getElementById('confirmpass_reg').value,
"bio" : document.getElementById('bio_reg').value
}
let response = await fetch('/newuser',
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data),
});
if(!response.ok){
throw new error("Encountered error when registering new user. Ensure that all fields are submitted and passwords match.");
}
} catch (error) {
alert(error);
}
console.log('Fetch happened')
});
// Post user profile information upon login
document.getElementById('form_login').addEventListener('submit', async function(event){
event.preventDefault();
try{
let data = {
"username" : document.getElementById('username_login').value,
}
let response = await fetch('/login',
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data),
});
console.log('response', response)
if(!response.ok){
throw new Error("Encountered error when logging in. Please ensure that all fields are completed.");
}
} catch (error) {
alert ("Problem: " + error);
}
console.log('Fetch happened')
});
//Post new forum post to post library (authentication required to execute post)
document.getElementById('form_create_post').addEventListener('submit', async function(event){
event.preventDefault();
try{
let data = {
"posttitle" : document.getElementById('post_title').value,
"postdate": document.getElementById('date').value,
"postcontent": document.getElementById('post_content').value
}
let response = await fetch('/createpost',
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data),
});
if(!response.ok){
console.log(response.code)
throw new Error("Encountered error creating new post. A post with this title already exists in the post library.");
}
} catch (error) {
alert ("Problem: " + error);
}
console.log('Fetch happened')
});
// Dynamically list current user directory
document.getElementById('listusers').addEventListener('click', async function(event){
let response = await fetch('/users');
let body = await response.text();
let userlist = JSON.parse(body);
$("#usercontent").html("");
for(let i = 0; i < userlist.length; i++){
usercontent.innerHTML += `
<br>
<div class="card bg-light" data-id=${userlist[i].username}>
<div class="card-body">
<h4 style="font-size: 22px; color: #16A2B8;">${userlist[i].fname} ${userlist[i].lname}</h4>
<h6 class="card-subtitle mb-2 ">${userlist[i].username}</h6>
<p><strong>Profile Bio:</strong> ${userlist[i].bio}</p>
</div>
</div>`
}
});
// Dynamically list current post library
document.getElementById('listposts').addEventListener('click', async function(event){
let response = await fetch('/posts');
let body = await response.text();
let postlist = JSON.parse(body);
$("#postlistcontent").html("");
for(let i = 0; i < postlist.length; i++){
postlistcontent.innerHTML += `
<br>
<div class="card bg-light" data-id=${postlist[i].posttitle}>
<div class="card-body">
<h4 style="font-size: 22px; color: #16A2B8;">${postlist[i].posttitle}</h4>
<h6 class="card-subtitle mb-2 text-muted"> Written by: ${postlist[i].postauthor}</h6>
<h6 class="card-subtitle mb-2 text-muted"><strong> ${postlist[i].postdate}</strong></h6>
<p>${postlist[i].postcontent}</p>
</div>
</div>`
}
});
// Dynamically list current user's posts
document.getElementById('mypostsbtn').addEventListener('click', async function(event){
let response = await fetch('/myposts');
let body = await response.text();
let postlist = JSON.parse(body);
$("#mypostslistcontent").html("");
for(let i = 0; i < postlist.length; i++){
mypostslistcontent.innerHTML += `
<br>
<div class="card bg-light" data-id=${postlist[i].posttitle}>
<div class="card-body">
<h4 style="font-size: 22px; color: #16A2B8;">${postlist[i].posttitle}</h4>
<h6 class="card-subtitle mb-2 text-muted"> Written by: ${postlist[i].postauthor}</h6>
<h6 class="card-subtitle mb-2 text-muted"><strong> ${postlist[i].postdate}</strong></h6>
<p>${postlist[i].postcontent}</p>
</div>
</div>`
}
});
// Generate current post feed
document.getElementById('postfeed').addEventListener('click', async function(event){
let response = await fetch('/posts');
let body = await response.text();
let postlist = JSON.parse(body);
$("#listallpostcontent").html("");
for(let i = 0; i < postlist.length; i++){
listallpostcontent.innerHTML += `
<br>
<div class="card bg-light" style="margin-left:-20px" data-id="${postlist[i].posttitle}">
<div class="card-body">
<h4 style="font-size: 22px; color: #16A2B8;">${postlist[i].posttitle}</h4>
<h6 class="card-subtitle mb-2 text-muted"> Written by: ${postlist[i].postauthor}</h6>
<h6 class="card-subtitle mb-2 text-muted"><strong> ${postlist[i].postdate}</strong></h6>
<p>${postlist[i].postcontent}</p>
</div>
</div>`
}
});