How do I create custom rules for authentication, f...
# help
b
How do I create custom rules for authentication, for example requiring that certain metadata exists?
n
Hello @benten! 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.
g
You could do a insert before trigger function on auth.users and return null if your conditions are not met.
n
benten (2022-04-18)
b
Sorry I'm very new to SQL, how does that work?
something like
Copy code
CREATE TRIGGER
  BEFORE INSERT ON auth.users
  FOR EACH ROW
  EXECUTE PROCEDURE
    //check if user_name exists and return null or true
?
g
https://supabase.com/docs/guides/auth/managing-user-data#advanced-techniques this shows the typical insert trigger on auth.users to generate a entries in a new table. Instead of doing the new entry stuff you would check new.??? for what you want then either "return new" if everything is OK or "return null" if not. Notice the trigger there is "after" that would be "before" in this case. I don't have an example for you on checking the new.metadata column, but it should be out there. The if user_name part will have to be formatted for json as it is part metadata. I'm out of time to google the format for the if then format, but it is out there or maybe in github supabase discussions.
b
Ok thank you for the help
g
new.raw_user_meta_data ->>'user_name' where user_name is what ever you pass in. I think.
b
Copy code
begin
  return case
    when raw_user_meta_data ->> 'user_name' = empty then null
    else new
  end;
end
this doesn't let anyone sign up
Copy code
begin
  return case
    when new.raw_user_meta_data ->> 'user_name' = '' then null
    else new
  end;
end
this lets everyone sign up lol
I got it working!
Copy code
create or replace function before_user_added()
returns trigger
language plpgsql
as $$
begin
  return case
    when new.raw_user_meta_data ->> 'user_name' = 'test' then null
    else new
  end;
end $$;

create trigger before_auth_user_created
  before insert on auth.users
  for each row execute procedure before_user_added()
Thanks again for the help
n
Thread was archived by @benten. Anyone can send a message to unarchive it.
2 Views