-1

I am trying to create a function and trigger to run on auth.users insert and cannot get the function to be applied to the table, it is a syntax error and I ma not to familiar with plpgsql.

If anyone has an idea where the error is please let me know I keep getting

Failed to run sql query: syntax error at or near ";"
drop function if exists handle_new_user();
create function handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
  if new.raw_user_meta_data::json->>'type' = 'practice' then
    insert into profiles (user_id, email, username, type, practice_id)
    values (
      new.id,
      new.email,
      new.raw_user_meta_data::json->>'username',
      new.raw_user_meta_data::json->>'type',
      new.id
    );
  else if new.raw_user_meta_data::json->>'type' = 'salesRep' then
    insert into profiles (user_id, email, username, type, rep_id)
    values (
      new.id,
      new.email,
      new.raw_user_meta_data::json->>'username',
      new.raw_user_meta_data::json->>'type',
      new.id
    );
  end if;
  return new;
end;
$$;

create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure handle_new_user();

Thank you so much

Luke Longo
  • 119
  • 1
  • 7

1 Answers1

0

I figured it out I needed to make the else if into elseif

Luke Longo
  • 119
  • 1
  • 7