From 6220af2ee334561f1545e3089a76f42da7b980bc Mon Sep 17 00:00:00 2001 From: Quinta <0pietroquintavalle0@gmail.com> Date: Sun, 25 Aug 2024 13:40:52 +0200 Subject: [PATCH] permissions --- components/GlyphGenerator.tsx | 39 ++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/components/GlyphGenerator.tsx b/components/GlyphGenerator.tsx index 55c3c1e..de0e41b 100644 --- a/components/GlyphGenerator.tsx +++ b/components/GlyphGenerator.tsx @@ -207,33 +207,49 @@ const GlyphGenerator = () => { return `${basePath}/glyphs/glyph${parseInt(glyph, 16) + 1}.webp`; }; - const loadUserData = useCallback(async(userId: string) => { + const loadUserData = useCallback(async (userId: string) => { + console.log("Loading user data for:", userId); const userRef = ref(database, `users/${userId}`); const snapshot = await get(userRef); if (snapshot.exists()) { const userData = snapshot.val(); setPortalAddress(userData.portalAddress || Array(12).fill('0')); setFriendshipCode(userData.friendshipCode || ''); + console.log("User data loaded successfully"); + } else { + console.log("No user data found"); } - loadGallery(); + await loadGallery(); }, []); useEffect(() => { + console.log("Setting up auth state listener"); const unsubscribe = onAuthStateChanged(auth, async (user) => { if (user) { + console.log("User authenticated:", user.uid); setUser(user); setUserId(user.uid); await loadUserData(user.uid); } else { - const anonymousId = localStorage.getItem('anonymousId') || Math.random().toString(36).substr(2, 9); - setUserId(anonymousId); - localStorage.setItem('anonymousId', anonymousId); + console.log("No user, attempting anonymous sign-in"); + try { + const anonymousUser = await signInAnonymously(auth); + console.log("Anonymous user signed in:", anonymousUser.user.uid); + setUser(anonymousUser.user); + setUserId(anonymousUser.user.uid); + await loadUserData(anonymousUser.user.uid); + } catch (error) { + console.error("Error signing in anonymously:", error); + showAlertMessage("Failed to authenticate. Please try again."); + } } - await loadGallery(); }); - return () => unsubscribe(); + return () => { + console.log("Cleaning up auth state listener"); + unsubscribe(); + }; }, [loadUserData]); const uploadImage = async (file: File | null) => { @@ -343,8 +359,14 @@ const GlyphGenerator = () => { }; const addToGallery = async () => { + if (!userId) { + showAlertMessage('You need to be logged in to add to the gallery.'); + return; + } + try { console.log("Starting to add to gallery..."); + console.log("Current user ID:", userId); console.log("Uploading images..."); const imageUrls = await Promise.all(images.map(img => uploadImage(img.file))); console.log("Image URLs obtained:", imageUrls); @@ -366,7 +388,9 @@ const GlyphGenerator = () => { console.log("New gallery item:", newGalleryItem); const newItemRef = push(ref(database, 'gallery')); + console.log("Attempting to set data in Firebase..."); await set(newItemRef, newGalleryItem); + console.log("Data successfully set in Firebase"); setDescription(''); setTags(''); @@ -381,6 +405,7 @@ const GlyphGenerator = () => { } }; + const handleVote = async (id: string, voteType: string) => { if (!userId) { showAlertMessage('You need to be logged in to vote.');