Run SQL migration

Apply the reviewed SQL changes to the database.
This commit is contained in:
gpt-engineer-app[bot] 2025-06-15 20:47:37 +00:00
parent 8c3b01e6b6
commit 9cd928329b
2 changed files with 50 additions and 4 deletions

View File

@ -56,16 +56,19 @@ const RegisterForm = ({ isLoading, setIsLoading }: RegisterFormProps) => {
if (error) { if (error) {
toast.error("Erro no cadastro", { 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) { } 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) { if (data.user.identities && data.user.identities.length === 0) {
toast.error("Erro no cadastro", { toast.info("E-mail já cadastrado. Verifique sua caixa de entrada!", {
description: "Este e-mail já está em uso. Tente fazer login.", 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 { } else {
toast.success("Cadastro realizado 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.", 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, duration: 10000,
}); });
} }

View File

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