is there a way i can get a user from auth.users? i...
# help
a
is there a way i can get a user from auth.users? i have a foreign-key for setting the user in a column, but i want to get that user's data (i.e. identity data)
g
Back to haunt you again... 😎 You have two choices, trigger and make a copy to a public table when inserted, or have an rpc function get the data when you need it (use security definer) and handle any protection you need. It really depends on how often you read this data, and protection you need.
a
I'll opt for the copy lol, i've already started on it anyways, but right now i'm having trouble trying to copy existing data over
g
hang on
sort of the same as your link, but "official"
a
right makes sense, im running this
Copy code
sql
do
$$
declare temprow record;
begin
  FOR temprow IN SELECT * FROM auth.users LOOP
    insert into public.profiles (id, email, preferred_name, identity_data, access)
    values (temprow.id, temprow.email, temprow.raw_user_meta_data.name, temprow.raw_user_meta_data, '');
  END LOOP;
end;
$$;
and i'm getting
missing FROM-clause entry for table "raw_user_meta_data"
assuming its that i'm trying to get the name form the metadata jsonb
oh nvm i got it to work
g
OK. I don't like your LOOP statement ... is this something you want to do on an insert to auth.user?
create or replace function public.handle_new_user() returns trigger as $$ begin insert into public."User" (id, email, username) values (new.id, new.email, new.raw_user_meta_data ->>'username'); return new; end; $$ language plpgsql security definer; is more along the lines for a trigger on auth.user insert (not my code so won't swear to the meta_data->> part...