From 8afd3920186f454c500caa96b760e11c01b08d98 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sat, 21 Jun 2025 14:13:41 +0000 Subject: [PATCH] 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. --- src/components/auth/SocialLoginButtons.tsx | 9 +++- .../20250621141237-fix-google-user-data.sql | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 supabase/migrations/20250621141237-fix-google-user-data.sql diff --git a/src/components/auth/SocialLoginButtons.tsx b/src/components/auth/SocialLoginButtons.tsx index 50dd9a3..3fa5a80 100644 --- a/src/components/auth/SocialLoginButtons.tsx +++ b/src/components/auth/SocialLoginButtons.tsx @@ -14,10 +14,17 @@ const SocialLoginButtons = () => { console.log('🔐 Iniciando login com Google...'); console.log('🌐 URL atual:', window.location.origin); + // Determinar a URL de redirecionamento correta + const redirectUrl = window.location.hostname === 'localhost' + ? 'http://localhost:3000/' + : `${window.location.origin}/`; + + console.log('🔗 URL de redirecionamento:', redirectUrl); + const { data, error } = await supabase.auth.signInWithOAuth({ provider: 'google', options: { - redirectTo: `${window.location.origin}/`, + redirectTo: redirectUrl, queryParams: { access_type: 'offline', prompt: 'consent', diff --git a/supabase/migrations/20250621141237-fix-google-user-data.sql b/supabase/migrations/20250621141237-fix-google-user-data.sql new file mode 100644 index 0000000..3448f05 --- /dev/null +++ b/supabase/migrations/20250621141237-fix-google-user-data.sql @@ -0,0 +1,54 @@ + +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; +$$;