Skip to content

Commit

Permalink
permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinta0 committed Aug 25, 2024
1 parent ca152d1 commit 6220af2
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions components/GlyphGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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('');
Expand All @@ -381,6 +405,7 @@ const GlyphGenerator = () => {
}
};


const handleVote = async (id: string, voteType: string) => {
if (!userId) {
showAlertMessage('You need to be logged in to vote.');
Expand Down

0 comments on commit 6220af2

Please sign in to comment.