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.
This commit is contained in:
gpt-engineer-app[bot] 2025-06-21 14:13:41 +00:00
parent a0eaf94ed5
commit 8afd392018
2 changed files with 62 additions and 1 deletions

View File

@ -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',

View File

@ -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;
$$;