-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.js
More file actions
248 lines (220 loc) · 9.53 KB
/
Copy pathauth.js
File metadata and controls
248 lines (220 loc) · 9.53 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
// Assuming 'auth' and 'db' are already initialized Firebase Authentication and Firestore instances
function setPermissions(email) {
var accordion = document.getElementById("addContentButton");
var editContentButton = document.getElementById("editContent");
var commentButton = document.getElementById("addCommentButton");
var participationButton = document.getElementById("participated");
var fullNameAndGrade = document.getElementById("FullNameAndGrade");
var participantsList = document.getElementById("participantsList");
var participantNumber = document.getElementById("participantNumber");
var report = document.getElementById("report");
var history = document.getElementById("history");
var bottomButtons = document.getElementById("bottomButtons");
var smallTextNameAndDate = document.querySelectorAll(".smallTextNameAndDate");
//defeat = comment not anonymous :)
smallTextNameAndDate.forEach(element => {
element.style.display = "block";
});
db.collection("users").doc(email).get().then(doc => {
if (doc.exists) {
const userData = doc.data();
console.log(userData);
const accountType = userData.accountType;
console.log(accountType);
switch(accountType) {
case "viewer":
fullNameAndGrade.style.display = "none";
participantsList.style.display = "none";
participantNumber.style.display = "flex";
accordion.style.display = "none";
editContentButton.style.display = "none";
commentButton.style.display = "none";
participationButton.style.display = "none";
bottomButtons.style.display = "none";
smallTextNameAndDate.forEach(element => {
element.style.display = "none";
});
break;
case "content creator":
fullNameAndGrade.style.display = "none";
participantsList.style.display = "none";
participantNumber.style.display = "flex";
editContentButton.style.display = "none";
commentButton.style.display = "flex";
participationButton.style.display = "flex";
accordion.style.display = "flex";
bottomButtons.style.display = "none";
break;
case "admin":
fullNameAndGrade.style.display = "flex";
participantsList.style.display = "block";
participantNumber.style.display = "block";
editContentButton.style.display = "flex";
commentButton.style.display = "none";
participationButton.style.display = "none";
accordion.style.display = "flex";
bottomButtons.style.display = "flex";
default:
break;
}
} else {
console.log("User data not found")
}
}).catch(error => {
console.error("Error getting user data:", error);
})
}
// function setAgeRestriction() {
// }
// Listen for auth state changes
auth.onAuthStateChanged(user => {
// Select elements outside the condition to avoid repetition
var signoutButton = document.querySelector('#signout-button');
var buttons = document.querySelectorAll("#nav-link");
var signedOutContent = document.getElementById('signedOutContent');
var bottomButtons = document.getElementById('bottomButtons'); // Ensure this element exists in your HTML
var signinButton = null;
var signupButton = null;
// Iterate to find sign in and sign up buttons
buttons.forEach(button => {
if (button.innerHTML.trim() === "Sign In") {
signinButton = button;
} else if (button.innerHTML.trim() === "Sign Up") {
signupButton = button;
}
});
// Ensure we have all necessary elements before proceeding
if (signoutButton && signinButton && signupButton && signedOutContent && bottomButtons) {
if (user && user.emailVerified) {
// User is signed in and email is verified
console.log("User verified:", user.emailVerified);
let toastMessage = document.getElementById("toastMessage");
let toastBody = document.getElementById("toastBody");
if (toastMessage && toastBody) {
toastMessage.classList.add("show");
toastBody.innerHTML = "Welcome (back) " + user.email;
}
console.log(user.email);
// Adjust button visibility based on user state
signoutButton.style.display = "flex";
bottomButtons.style.display = "flex";
signinButton.style.display = "none";
signupButton.style.display = "none";
signedOutContent.style.display = "none";
// Fetch user data or set permissions
db.collection("users").doc(user.email).get().then(doc => {
if (doc.exists) {
let accountType = returnPermissions(user.email);
console.log("Setting account type:", accountType);
// User data exists, handle accordingly
console.log("Document data:", doc.data());
}
}).catch(error => {
console.error("Error getting user data:", error);
});
setPermissions(user.email); // Assuming this is a function to set permissions based on user's email
} else {
// Handle signed-out state or email not verified
bottomButtons.style.display = "none";
signoutButton.style.display = "none";
signinButton.style.display = "flex";
signupButton.style.display = "flex";
signedOutContent.style.display = "block";
if (typeof hideContent === "function") {
hideContent('contentContainer'); // Ensure this function is defined and safely callable
}
}
} else {
console.error("Some UI elements could not be found.");
}
});
// Utility function for hiding content by class
function hideContent(hiddenClass) {
let elements = document.getElementsByClassName(hiddenClass);
Array.from(elements).forEach(element => {
element.style.display = "none";
});
}
// Function to determine user permissions
function returnPermissions(email) {
if (email.endsWith("@pinewood.edu")) {
if (/^[^\d]/.test(email)) {
return "admin";
} else {
return "content creator";
}
} else {
return "viewer";
}
}
// Sign-In Form Event Listener
const signinform = document.querySelector('#signin-form');
signinform.addEventListener('submit', e => {
e.preventDefault();
const email = signinform['emailInputSignIn'].value;
const password = signinform['passwordInputSignIn'].value;
auth.signInWithEmailAndPassword(email, password)
.then(cred => {
// Assuming you want to reset and redirect here
signinform.reset();
if (cred.user.emailVerified) {
setPermissions(email);
window.location.href = "index.html"; // Consider using a more dynamic approach or SPA routing
}
else {
console.error("Please verify your email!")
}
})
.catch(error => {
alert(error.message);
});
});
// Sign-Up Form Event Listener
const signupForm = document.querySelector('#signup-form');
signupForm.addEventListener('submit', e => {
e.preventDefault();
const email = signupForm['emailInputSignUp'].value.toLowerCase();
const password = signupForm['passwordInputSignUp'].value;
const fullName = signupForm['fullNameInputSignUp'].value;
const grade = signupForm['gradeInputSignUp'].value;
auth.createUserWithEmailAndPassword(email, password).then(cred => {
// Send an email verification to the newly created user
cred.user.sendEmailVerification().then(() => {
// Email sent.
console.log("Verification email sent.");
}).catch(verificationError => {
// Handle Errors here.
console.error("Error sending verification email:", verificationError);
});
// Set user data in Firestore
return db.collection("users").doc(email).set({
name: fullName,
grade: grade,
accountType: returnPermissions(email) // Use the returnPermissions function to set account type
});
}).then(() => {
//window.location.href = "index.html";
alert("Sign Up Successful! Please verify your email before logging in.");
}).catch(error => {
alert(error.message);
});
});
// Sign-Out Event Listener
const signout = document.querySelector('#signout-button');
signout.addEventListener('click', e => {
e.preventDefault();
auth.signOut().then(() => {
window.location.href = "index.html";
}).catch(error => {
alert(error.message);
});
});
// // Google Sign-In
// var provider = new firebase.auth.GoogleAuthProvider();
// document.getElementById('google-signin-btn').addEventListener('click', function() {
// firebase.auth().signInWithPopup(provider).then(result => {
// window.location.href = "index.html";
// }).catch(error => {
// alert(error.message);
// });
// });