Skip to content

Commit e58ac86

Browse files
authored
Fix admin check, add admin func, add debug logs (#34)
1 parent 25ec77d commit e58ac86

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,8 @@ To initialise the auctions:
119119
- With the device you used to create your admin account, head to your website and navigate to the admin page by clicking the `Admin` button in the top right.
120120
- Open the developer console (F12) and enter `resetAll()` into the console.
121121
- This will revert the entire auction to the initial state specified in `js/firebase.js` (as long as you are admin), be careful with this one!
122-
- You can also reset individual items using the `resetItem(i)` function.
123-
- You can also use this `Admin` page to monitor the status of your auction.
122+
123+
While on this `Admin` page you can also:
124+
- Monitor the status of your auction.
125+
- Reset individual items using the `resetItem(i)` function.
126+
- Reset all user's admin status using `resetUsers()` (excluding the current user).

js/admin.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { db } from "./firebase.js";
1+
import { auth, db } from "./firebase.js";
22
import { getItems, isDemo } from "./items.js";
33
import { timeToString, dataListener } from "./auctions.js";
44
import {
55
doc,
66
setDoc,
77
getDoc,
8+
getDocs,
89
updateDoc,
10+
collection,
11+
writeBatch,
912
deleteField,
1013
Timestamp,
1114
} from "https://www.gstatic.com/firebasejs/9.20.0/firebase-firestore.js";
@@ -126,5 +129,33 @@ function resetAll() {
126129
});
127130
}
128131

132+
async function resetUsers() {
133+
const users = await getDocs(collection(db, "users"));
134+
let batches = [];
135+
users.forEach((user) => {
136+
// Add new writeBatch if:
137+
// - there are no batches
138+
// - the current batch is full
139+
if (
140+
batches.length == 0 ||
141+
batches[batches.length - 1]._mutations.length % 500 == 0
142+
) {
143+
batches.push(writeBatch(db));
144+
}
145+
// Add write to batch if not the current user
146+
if (user.id != auth.currentUser.uid) {
147+
const userRef = doc(db, "users", user.id);
148+
batches[batches.length - 1].update(userRef, { admin: "" });
149+
} else {
150+
console.debug("Not resetting current user");
151+
}
152+
});
153+
// Commit all batches
154+
for await (const batch of batches) {
155+
await batch.commit();
156+
}
157+
}
158+
129159
window.resetItem = resetItem;
130160
window.resetAll = resetAll;
161+
window.resetUsers = resetUsers;

js/popups.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ const signUpModalSubmit = signUpModal.querySelector(".btn-primary");
2323
export function autoSignIn() {
2424
onAuthStateChanged(auth, (user) => {
2525
if (user && user.displayName != null) {
26+
console.debug(`Signed-in: name=${user.displayName}, uid=${user.uid}`);
2627
// If user has an anonymous account and a displayName, treat them as signed in
2728
authButton.innerText = "Sign out";
2829
document.getElementById("username-display").innerText =
2930
"Hi " + user.displayName;
3031
// If user is admin, display the admin button
3132
getDoc(doc(db, "users", user.uid)).then((user) => {
32-
if ("admin" in user.data()) {
33+
if (user.data().admin) {
34+
console.debug("User is admin");
3335
adminButton.style.display = "inline-block";
3436
}
3537
});
@@ -72,7 +74,7 @@ function signUp() {
7274
let username = signUpModalInput;
7375
let user = auth.currentUser;
7476
updateProfile(user, { displayName: username.value });
75-
setDoc(doc(db, "users", user.uid), { name: username.value, admin: false });
77+
setDoc(doc(db, "users", user.uid), { name: username.value, admin: "" });
7678
console.debug("signUp() write to users/${auth.currentUser.uid}");
7779
authButton.innerText = "Sign out";
7880
document.getElementById("username-display").innerText =
@@ -170,6 +172,7 @@ if (bidModal) {
170172
let item = data[bids[0]];
171173
let bidId = `bid${bids.length.toString().padStart(5, "0")}`;
172174
let currentBid = data[bids[bids.length - 1]].amount;
175+
console.debug(`itemId=${itemId} currentBid=${currentBid}`);
173176
if (amount >= 1 + currentBid) {
174177
updateDoc(docRef, {
175178
[`${itemId}_${bidId}`]: {

0 commit comments

Comments
 (0)