Hi, having troubles with using a trigger to create...
# help
m
Hi, having troubles with using a trigger to create a user.profile entry when a user signs up. My sign up:
Copy code
const { user, session, error } = await supabase.auth.signUp(
      {
        email: email,
        password: password,
      },
      {
        data: {
          firstName,
          lastName,
          email,
          rating: 0,
          profilePicURL: "",
        },
      }
    );
This adds to the auth table:
n
Hello @msy! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
m
my trigger, and function:
Copy code
create or replace function public.handle_new_user() 
returns trigger as $$
begin
  insert into public.users (id, email, firstName)
  values (new.id, new.email, new.raw_user_meta_data ->> 'firstName');
  return new;
end;
$$ language plpgsql security definer;

create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();
Note: it works when I just use
new.email
in the line
insert into public.users (id, email, firstName)
However, once I add
new.raw_user_meta_data ->> 'firstName');
the sign up stops working and I recieve a post 500 error when attempting to sign up
as you can see in the actual auth.users entry:
firstName
does exist. Not exactly sure what is going wrong here.
g
Did you just call out everyone?
n
MSY (2022-04-29)
m
Sorry i was just checking the roles and I accidentally pressed enter my bad
I deleted it
g
I think your problem is you have firstName as a column name with a capital letter and no "firstName" with double quotes. BUT you should use all lower case in postgres unless you are forced to use capitals as it will bite you in the long run. Postgres treats firstName with out quotes as firstname and won't match firstName if that is your intended column name.
m
that was the issue! thank u :) sorry for the ping!!
it works now
g
If you just stuck quotes around it, I highly encourage you to use first_name as the column name. The only real reason to not do that is if you are on Prisma or some higher level database tool that forces camel case.
m
I ended up renaming all the columns to lower case
g
👍
m
Copy code
const { user: authUser } = await supabase.auth.api.getUserByCookie(req);
  // Check if the user is logged
  if (authUser === null) {
    // Redirect if no logged in
    return { props: {}, redirect: { destination: "/" } };
  }
  // If logged return the user
  let { user, error } = await supabase.from("users").select("id, first_name");
  console.log("user", authUser);
  if (user) {
    return { props: { user: user } };
  } else {
    return { props: {}, redirect: { destination: "/" } };
  }
I'm trying to do this now, but it's not returning anything for the user?
It does successfully get the authUser though
I read on the supabase documentation that with the set policies I wouldn't need to filter and I could directly do .from("users") and get the relevant row from the public users table, with the primary key set as a foreign key to the auth.users table
g
Please start a new question as you are now into server side request and jwt token stuff which had nothing to do with the original question.
m
/newquestion
How do I do that
ohh make a new thread
One sec