I'm trying to add rows to `profiles` table when ne...
# off-topic
l
I'm trying to add rows to
profiles
table when new users are added, including metadata like
email address
,
name
,
website
, all through the initial sign up function. So creating a new user like this:
Copy code
javascript
      const { data, error } = await supabase.auth.signIn(
        { email: localStorage.getItem('email') },
        {
          data: {
            full_name: formData.get('full_name'),
            introduction: formData.get('introduction'),
            website_url: formData.get('website_url'),
            linkedin_url: formData.get('linkedin_url'),
            twitter_handle: formData.get('twitter_handle'),
            contact_method: formData.get('contact_method')
        }
      })
And handling it like this:
Copy code
sql
create function public.handle_new_profile()
returns trigger as $$
begin
    insert into public.profiles (user_id, user_email, full_name, introduction, website_url, linkedin_url, twitter_handle, contact_method )
    values (new.id, new.email, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'introduction', new.raw_user_meta_data->>'website_url', new.raw_user_meta_data->>'linkedin_url', new.raw_user_meta_data->>'twitter_handle', new.raw_user_meta_data->>'contact_method');
    return new;
end;
$$ language plpgsql security definer;

-- trigger the function every time a user is created
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_profile();
Currently, new rows in
profiles
are created when the function is called — but they only contain the
user_id
and
user_email
fields, none of the other data. I feel like this might be some syntax error in the
public.handle_new_profile()
function, not receiving the user metadata or int it the right way. Can anybody suggest a troubleshoot/fix?
y
you're using magic links right? the signIn method doesn't have a data property in options so I think you can't add metadata when you sign in via magic links
you could probably verify this by checking your
auth.users
table and looking at the raw_user_meta_data column to see if it's populated
to do this just goto the shell and run
select * from auth.users
@User
l
Hey YelloJello, thanks so much for following up on this. Indeed, using
select * from auth.users
, I'm not seeing anything in the raw_user_meta_data column.
Yes, using magic links, to avoid the need to create a password. Basically, the use case is that I'm trying to provide a form that will capture: * email * name * website * some other links I don't need authentication right now, so I'd like to just be able to capture that form data and store it in
profiles
, for when more of the application is ready later — instead of needing a separate form handler. Does that make sense? And any other thoughts on how I could do this?
s
Ah I should have noticed that there was no data property in the second param to signIn
I guess your other option is to take only the signIn now and then ask for the remaining information when the user first signs in
m
❤️ this is awesome! This is just what I was looking for. There was not documentation about this. At first I was trying
user_meta_data
but the correct name is
raw_user_meta_data
!!!!
BTW, it seems
language plpgsql security definer;
should not be there. It should be something like this:
Copy code
-- inserts a row into public.users
create function public.handle_new_user() 
returns trigger 
language plpgsql 
security definer set search_path = public
as $$
begin
  insert into public.profiles (id)
  values (new.id);
  return new;
end;
$$;