postgresqlsupabase

Add aditional user information when signup supabase


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'
      }
    });

  }

Solution

  • 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',
     'example@mail.com', 
     '$2y$10$WpAA2remsZRnZivgRaM9L.1BcjvAtUa966AICxv1RGte68BICsZkS',
     '{"username": "user_example"}'
    ) 
    

    Final solution:

     const { user, session, error } = await supabase.auth.signUp(
    {
      email: 'example@email.com',
      password: 'example-password',
    },
    {
      data: { 
        username: 'John'(variable)
      }
    }
    )