budget-view-finance/supabase/migrations/20250621141237-fix-google-user-data.sql
gpt-engineer-app[bot] 8afd392018 Fix: Redirect after Google login
The user is redirected to a "page not accessible" error after Google login in production. The email is saved in Supabase, but name and WhatsApp are missing. The app should redirect to the profile completion page to collect the missing data.
2025-06-21 14:13:41 +00:00

55 lines
1.3 KiB
PL/PgSQL

CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = 'public'
AS $$
DECLARE
nome_val TEXT;
empresa_val TEXT;
whatsapp_val TEXT;
BEGIN
-- Extrair nome do Google (full_name ou name dos metadados)
nome_val := COALESCE(
new.raw_user_meta_data->>'full_name',
new.raw_user_meta_data->>'name',
new.raw_user_meta_data->>'nome',
'Nome não informado'
);
-- Extrair empresa (se fornecida)
empresa_val := new.raw_user_meta_data->>'empresa';
-- WhatsApp padrão (será preenchido no complete-profile)
whatsapp_val := COALESCE(new.raw_user_meta_data->>'whatsapp', '00000000000');
-- Log dos dados recebidos para debug
RAISE LOG 'Processando novo usuário: email=%, nome=%, metadados=%',
new.email, nome_val, new.raw_user_meta_data;
-- Tentar atualizar usuário existente primeiro
UPDATE public.usuarios
SET
id = new.id,
nome = nome_val,
empresa = empresa_val,
whatsapp = whatsapp_val
WHERE email = new.email;
-- Se não encontrou, inserir novo usuário
IF NOT FOUND THEN
INSERT INTO public.usuarios (id, email, nome, empresa, whatsapp)
VALUES (
new.id,
new.email,
nome_val,
empresa_val,
whatsapp_val
);
END IF;
RETURN new;
END;
$$;