Skip to content

Commit

Permalink
Fix admin check, add admin func, add debug logs (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmellor authored Jul 19, 2023
1 parent 25ec77d commit e58ac86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,8 @@ To initialise the auctions:
- 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.
- Open the developer console (F12) and enter `resetAll()` into the console.
- 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!
- You can also reset individual items using the `resetItem(i)` function.
- You can also use this `Admin` page to monitor the status of your auction.

While on this `Admin` page you can also:
- Monitor the status of your auction.
- Reset individual items using the `resetItem(i)` function.
- Reset all user's admin status using `resetUsers()` (excluding the current user).
33 changes: 32 additions & 1 deletion js/admin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { db } from "./firebase.js";
import { auth, db } from "./firebase.js";
import { getItems, isDemo } from "./items.js";
import { timeToString, dataListener } from "./auctions.js";
import {
doc,
setDoc,
getDoc,
getDocs,
updateDoc,
collection,
writeBatch,
deleteField,
Timestamp,
} from "https://www.gstatic.com/firebasejs/9.20.0/firebase-firestore.js";
Expand Down Expand Up @@ -126,5 +129,33 @@ function resetAll() {
});
}

async function resetUsers() {
const users = await getDocs(collection(db, "users"));
let batches = [];
users.forEach((user) => {
// Add new writeBatch if:
// - there are no batches
// - the current batch is full
if (
batches.length == 0 ||
batches[batches.length - 1]._mutations.length % 500 == 0
) {
batches.push(writeBatch(db));
}
// Add write to batch if not the current user
if (user.id != auth.currentUser.uid) {
const userRef = doc(db, "users", user.id);
batches[batches.length - 1].update(userRef, { admin: "" });
} else {
console.debug("Not resetting current user");
}
});
// Commit all batches
for await (const batch of batches) {
await batch.commit();
}
}

window.resetItem = resetItem;
window.resetAll = resetAll;
window.resetUsers = resetUsers;
7 changes: 5 additions & 2 deletions js/popups.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ const signUpModalSubmit = signUpModal.querySelector(".btn-primary");
export function autoSignIn() {
onAuthStateChanged(auth, (user) => {
if (user && user.displayName != null) {
console.debug(`Signed-in: name=${user.displayName}, uid=${user.uid}`);
// If user has an anonymous account and a displayName, treat them as signed in
authButton.innerText = "Sign out";
document.getElementById("username-display").innerText =
"Hi " + user.displayName;
// If user is admin, display the admin button
getDoc(doc(db, "users", user.uid)).then((user) => {
if ("admin" in user.data()) {
if (user.data().admin) {
console.debug("User is admin");
adminButton.style.display = "inline-block";
}
});
Expand Down Expand Up @@ -72,7 +74,7 @@ function signUp() {
let username = signUpModalInput;
let user = auth.currentUser;
updateProfile(user, { displayName: username.value });
setDoc(doc(db, "users", user.uid), { name: username.value, admin: false });
setDoc(doc(db, "users", user.uid), { name: username.value, admin: "" });
console.debug("signUp() write to users/${auth.currentUser.uid}");
authButton.innerText = "Sign out";
document.getElementById("username-display").innerText =
Expand Down Expand Up @@ -170,6 +172,7 @@ if (bidModal) {
let item = data[bids[0]];
let bidId = `bid${bids.length.toString().padStart(5, "0")}`;
let currentBid = data[bids[bids.length - 1]].amount;
console.debug(`itemId=${itemId} currentBid=${currentBid}`);
if (amount >= 1 + currentBid) {
updateDoc(docRef, {
[`${itemId}_${bidId}`]: {
Expand Down

0 comments on commit e58ac86

Please sign in to comment.