diff --git a/src/components/auth/RegisterForm.tsx b/src/components/auth/RegisterForm.tsx index b396a49..d346220 100644 --- a/src/components/auth/RegisterForm.tsx +++ b/src/components/auth/RegisterForm.tsx @@ -1,5 +1,6 @@ import { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; import { Button } from "@/components/ui/button"; import { formatarWhatsapp } from '@/utils/whatsappFormatter'; import { validateRegisterForm } from '@/utils/registerValidation'; @@ -14,6 +15,7 @@ interface RegisterFormProps { } const RegisterForm = ({ isLoading, setIsLoading }: RegisterFormProps) => { + const navigate = useNavigate(); const [nome, setNome] = useState(''); const [empresa, setEmpresa] = useState(''); const [email, setEmail] = useState(''); @@ -72,38 +74,33 @@ const RegisterForm = ({ isLoading, setIsLoading }: RegisterFormProps) => { }); } else { console.log('✅ Novo usuário cadastrado com sucesso'); - toast.success("Cadastro realizado com sucesso!", { - description: "Enviamos um link de confirmação para o seu e-mail. Por favor, verifique sua caixa de entrada e spam para ativar sua conta.", - duration: 10000, - }); - // Send webhook to n8n workflow manager - NEW APPROACH + // Send webhook to n8n workflow manager in background console.log('📡 Enviando webhook para gerenciador n8n...'); console.log('📋 Email do usuário:', email); console.log('📋 ID do usuário:', data.user.id); + console.log('📋 WhatsApp do usuário:', whatsapp); - try { - const webhookSuccess = await sendNewUserWebhook(email, data.user.id); - if (webhookSuccess) { - console.log('✅ Webhook enviado com sucesso para n8n'); - toast.success("Configuração automática iniciada!", { - description: "Sua conta foi criada e a configuração automática do sistema foi iniciada.", - duration: 8000, - }); - } else { - console.error('❌ Falha no envio do webhook para n8n'); - toast.error("Aviso: Configuração", { - description: "Cadastro realizado, mas houve falha na configuração automática. Entre em contato com o suporte.", - duration: 10000, - }); - } - } catch (webhookError) { - console.error('❌ Erro crítico no webhook para n8n:', webhookError); - toast.error("Aviso: Configuração", { - description: "Cadastro realizado, mas houve falha na configuração automática. Entre em contato com o suporte.", - duration: 10000, + // Send webhook in background (don't wait for it) + sendNewUserWebhook(email, data.user.id, whatsapp.replace(/\D/g, '')) + .then((success) => { + if (success) { + console.log('✅ Webhook enviado com sucesso para n8n'); + } else { + console.error('❌ Falha no envio do webhook para n8n'); + } + }) + .catch((error) => { + console.error('❌ Erro crítico no webhook para n8n:', error); }); - } + + // Redirect to login page with success message + navigate('/auth', { + state: { + showSuccessMessage: true, + message: "✅ Cadastro realizado com sucesso! Agora, faça o login com o e-mail e a senha que você acabou de criar." + } + }); } } } catch (error) { diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index 4bd755e..2224486 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -512,6 +512,7 @@ export type Database = { status_instancia: string | null webhook: string | null whatsapp: string + workflow_id: string | null } Insert: { created_at?: string @@ -525,6 +526,7 @@ export type Database = { status_instancia?: string | null webhook?: string | null whatsapp: string + workflow_id?: string | null } Update: { created_at?: string @@ -538,6 +540,7 @@ export type Database = { status_instancia?: string | null webhook?: string | null whatsapp?: string + workflow_id?: string | null } Relationships: [] } diff --git a/src/pages/Auth.tsx b/src/pages/Auth.tsx index 97e879c..eb29d96 100644 --- a/src/pages/Auth.tsx +++ b/src/pages/Auth.tsx @@ -1,13 +1,30 @@ -import { useState } from 'react'; -import { Link } from 'react-router-dom'; +import { useState, useEffect } from 'react'; +import { Link, useLocation } from 'react-router-dom'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import LoginForm from '@/components/auth/LoginForm'; import RegisterForm from '@/components/auth/RegisterForm'; +import { toast } from "sonner"; const Auth = () => { const [isLoading, setIsLoading] = useState(false); + const [activeTab, setActiveTab] = useState("login"); + const location = useLocation(); + + // Show success message when redirected from registration + useEffect(() => { + if (location.state?.showSuccessMessage && location.state?.message) { + toast.success("Cadastro realizado!", { + description: location.state.message, + duration: 8000, + }); + // Set active tab to login + setActiveTab("login"); + // Clear the state to prevent showing the message again + window.history.replaceState({}, document.title); + } + }, [location.state]); return (
@@ -17,7 +34,7 @@ const Auth = () => { Gerencie suas finanças de forma simples e eficiente - + Login Cadastro diff --git a/src/services/newUserWebhookService.ts b/src/services/newUserWebhookService.ts index 972ca19..aa04dcf 100644 --- a/src/services/newUserWebhookService.ts +++ b/src/services/newUserWebhookService.ts @@ -5,24 +5,28 @@ import { supabase } from "@/integrations/supabase/client"; interface NewUserWebhookData { email: string; userId: string; + whatsapp: string; } /** * Sends a webhook to the n8n workflow manager when a new user registers * @param email User's email * @param userId User's ID from Supabase Auth + * @param whatsapp User's WhatsApp number * @returns Success status */ export async function sendNewUserWebhook( email: string, - userId: string + userId: string, + whatsapp: string ): Promise { try { - console.log(`📡 Enviando webhook para n8n - Usuário: ${email}, ID: ${userId}`); + console.log(`📡 Enviando webhook para n8n - Usuário: ${email}, ID: ${userId}, WhatsApp: ${whatsapp}`); const webhookData: NewUserWebhookData = { email: email, - userId: userId + userId: userId, + whatsapp: whatsapp }; console.log('📋 Dados do webhook:', JSON.stringify(webhookData, null, 2));