From 196d3e921132212b5a15d62a9b122ba62957b68d Mon Sep 17 00:00:00 2001 From: dwindown Date: Thu, 25 Dec 2025 23:35:40 +0700 Subject: [PATCH] Fix review name capture with auth metadata fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes: - ReviewModal now fetches name from profiles, auth metadata, or email - Added console logging to debug review data - Falls back to email username if no name found - This ensures reviewer_name is always populated For existing reviews without names, run: UPDATE reviews r SET reviewer_name = ( SELECT COALESCE( raw_user_meta_data->>'name', SPLIT_PART(email, '@', 1) ) FROM auth.users WHERE id = r.user_id ) WHERE reviewer_name IS NULL; 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/components/reviews/ProductReviews.tsx | 2 ++ src/components/reviews/ReviewModal.tsx | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/components/reviews/ProductReviews.tsx b/src/components/reviews/ProductReviews.tsx index 1c46690..1e37028 100644 --- a/src/components/reviews/ProductReviews.tsx +++ b/src/components/reviews/ProductReviews.tsx @@ -41,6 +41,8 @@ export function ProductReviews({ productId, type }: ProductReviewsProps) { const { data } = await query.order('created_at', { ascending: false }).limit(3); + console.log('Raw reviews data:', data); + if (data && data.length > 0) { const typedData = data as unknown as Review[]; setReviews(typedData); diff --git a/src/components/reviews/ReviewModal.tsx b/src/components/reviews/ReviewModal.tsx index 76bb108..a818509 100644 --- a/src/components/reviews/ReviewModal.tsx +++ b/src/components/reviews/ReviewModal.tsx @@ -38,13 +38,27 @@ export function ReviewModal({ useEffect(() => { const fetchUserName = async () => { if (!userId) return; - const { data } = await supabase + + // Try to get name from profiles table first + const { data: profileData } = await supabase .from('profiles') .select('name') .eq('id', userId) - .single(); - if (data?.name) { - setUserName(data.name); + .maybeSingle(); + + if (profileData?.name) { + setUserName(profileData.name); + return; + } + + // Fallback: get from auth metadata + const { data: { user } } = await supabase.auth.getUser(); + if (user?.user_metadata?.name) { + setUserName(user.user_metadata.name); + } else if (user?.email) { + // Use email username as last resort + const emailUsername = user.email.split('@')[0]; + setUserName(emailUsername); } }; fetchUserName();