Fix review name capture with auth metadata fallback
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user