diff --git a/src/components/auth/RegisterForm.tsx b/src/components/auth/RegisterForm.tsx index ddc0358..d4c8411 100644 --- a/src/components/auth/RegisterForm.tsx +++ b/src/components/auth/RegisterForm.tsx @@ -56,16 +56,19 @@ const RegisterForm = ({ isLoading, setIsLoading }: RegisterFormProps) => { if (error) { toast.error("Erro no cadastro", { - description: error.message || "Não foi possível completar o cadastro. O e-mail já pode estar em uso.", + description: error.message || "Não foi possível completar o cadastro. Por favor, tente novamente.", }); } else if (data.user) { + // This condition (empty identities) indicates that the user already exists in Supabase Auth. + // In this case, `signUp` resends the confirmation/invitation email. if (data.user.identities && data.user.identities.length === 0) { - toast.error("Erro no cadastro", { - description: "Este e-mail já está em uso. Tente fazer login.", + toast.info("E-mail já cadastrado. Verifique sua caixa de entrada!", { + description: "Enviamos um novo link para você definir sua senha e acessar sua conta. Não se esqueça de checar a pasta de spam.", + duration: 10000, }); } else { 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.", + 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, }); } diff --git a/supabase/migrations/20250615204657-2e288caf-c8a1-46b4-959a-c2ecfaa24986.sql b/supabase/migrations/20250615204657-2e288caf-c8a1-46b4-959a-c2ecfaa24986.sql new file mode 100644 index 0000000..a185263 --- /dev/null +++ b/supabase/migrations/20250615204657-2e288caf-c8a1-46b4-959a-c2ecfaa24986.sql @@ -0,0 +1,43 @@ + +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 + -- Extract values from metadata, providing default placeholders if they are missing + nome_val := COALESCE(new.raw_user_meta_data->>'nome', 'Nome não informado'); + empresa_val := new.raw_user_meta_data->>'empresa'; -- This field can be null + whatsapp_val := COALESCE(new.raw_user_meta_data->>'whatsapp', '00000000000'); + + -- This logic attempts to update an existing user record found by email. + -- This is key to reconnecting your old data to your new login. + UPDATE public.usuarios + SET + id = new.id, + nome = nome_val, + empresa = empresa_val, + whatsapp = whatsapp_val + WHERE email = new.email; + + -- If no record was updated (because the email wasn't found), + -- it means this is a completely new user, so we insert a new record. + 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; +$$;