5

I am trying to add additional data in a user record in supabase, I have created a trigger that is called after a record has been inserted in auth user that should add the user id and username in the profiles table. This happens but it doesn't add the user name, it's still null in the profiles table. That data is supposed to go in the raw_user_meta_data column but it still doesn't add in the column

Trigger function:

BEGIN
INSERT INTO public.profiles(id, username)
VALUES (
 NEW.id,
 NEW.raw_user_meta_data -> 'username'
);
RETURN NEW;
END;

Front:

    const createNewUser = async() => {
    const { username, email, password } = credentials;

    await supabase.auth.signUp({
      email: email,
      password: password,
      data: {
        "username": 'hello'
      }
    });

  }

2 Answers 2

8

Just to follow up. Maybe this is the change with supabase v2 and supabasejs update, but now it seems to work with one argument, but slightly different than you had it in first post. Here is the link: https://supabase.com/docs/reference/javascript/auth-signup#sign-up-with-additional-user-metadata

and the code:

const { data, error } = await supabase.auth.signUp({
      email: userEmail.value,
      password: password,
      options: {
        data: {
          user_name: userName.value,
        },
      },
    });
1
  • I was having an issue with the accepted answer. It seems Supabase updated the function. This way worked for me.
    – James
    Jan 21, 2023 at 22:15
6

I found the solution reading over there, in case it helps anyone. My mistake was that in the signUp function I passed only 1 argument that had included the additional data that would be included in the trigger function. However, this signUp function must be passed 2 objects as arguments and it is this second argument that is passed that saves the object with the username extracted from the function in the front in the raw_user_meta_data column.

As additional data that can help you in the search for the error. You can know what the logs of the authentication process print. You can also insert a record directly in auth.users so you can see why it does not add additional data and be able to review the logs, I attach an example:

insert into auth.users(id, email, encrypted_password, raw_user_meta_data)
values (
 'eb23060d-71ea-4112-a1c7-203d6f87fa2d',
 '[email protected]', 
 '$2y$10$WpAA2remsZRnZivgRaM9L.1BcjvAtUa966AICxv1RGte68BICsZkS',
 '{"username": "user_example"}'
) 

Final solution:

 const { user, session, error } = await supabase.auth.signUp(
{
  email: '[email protected]',
  password: 'example-password',
},
{
  data: { 
    username: 'John'(variable)
  }
}
)
1
  • 2
    wow, thanks for the node on the two arguments towards signUp. I missed that and spent a lot of time searching untill I arrived at your comment :) Aug 4, 2022 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.