Files
thrilltrack-explorer/supabase/migrations/20251030202332_3f9f8627-04d3-4dbb-a17d-7db21169eeb2.sql
2025-10-30 20:23:42 +00:00

43 lines
1007 B
PL/PgSQL

-- Fix type mismatch in get_users_with_emails function
CREATE OR REPLACE FUNCTION public.get_users_with_emails()
RETURNS TABLE (
id uuid,
user_id uuid,
username text,
email text,
display_name text,
avatar_url text,
banned boolean,
created_at timestamptz
)
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path TO 'public', 'auth'
AS $$
BEGIN
-- Check if caller is superuser or admin
IF NOT EXISTS (
SELECT 1 FROM public.user_roles
WHERE user_roles.user_id = auth.uid()
AND role IN ('superuser', 'admin')
) THEN
RAISE EXCEPTION 'Access denied: requires admin or superuser role'
USING ERRCODE = '42501';
END IF;
-- Return profiles with emails from auth.users
RETURN QUERY
SELECT
p.id,
p.user_id,
p.username,
COALESCE(au.email, 'unknown@email.com')::text as email,
p.display_name,
p.avatar_url,
p.banned,
p.created_at
FROM public.profiles p
LEFT JOIN auth.users au ON au.id = p.user_id
ORDER BY p.created_at DESC;
END;
$$;