© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•13mo ago•
13 replies
SY

Prevent supabase client updateUser (need help with trigger)

I'm trying to create a trigger that prevents updateUser from supabase client unless its auth.admin ('serviceRole')

CREATE OR REPLACE FUNCTION prevent_restricted_user_updates()
RETURNS TRIGGER AS $$
BEGIN
  IF current_setting('request.jwt.claim.sub', true) = 'service_role' THEN
    RETURN NEW;
  END IF;

  IF NEW.user_metadata IS DISTINCT FROM OLD.user_metadata OR
     NEW.email IS DISTINCT FROM OLD.email OR
     NEW.phone IS DISTINCT FROM OLD.phone THEN
    RAISE EXCEPTION 'Updating user_metadata, email, or phone is not allowed.';
  END IF;

  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER block_restricted_user_updates
BEFORE UPDATE ON auth.users
FOR EACH ROW
EXECUTE FUNCTION prevent_restricted_user_updates();
CREATE OR REPLACE FUNCTION prevent_restricted_user_updates()
RETURNS TRIGGER AS $$
BEGIN
  IF current_setting('request.jwt.claim.sub', true) = 'service_role' THEN
    RETURN NEW;
  END IF;

  IF NEW.user_metadata IS DISTINCT FROM OLD.user_metadata OR
     NEW.email IS DISTINCT FROM OLD.email OR
     NEW.phone IS DISTINCT FROM OLD.phone THEN
    RAISE EXCEPTION 'Updating user_metadata, email, or phone is not allowed.';
  END IF;

  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER block_restricted_user_updates
BEFORE UPDATE ON auth.users
FOR EACH ROW
EXECUTE FUNCTION prevent_restricted_user_updates();


this seems to do the trick, but messes up signin and signup.

whats wrong?
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

updateUser error
SupabaseSSupabase / help-and-questions
4mo ago
Help with a trigger
SupabaseSSupabase / help-and-questions
4y ago
Supabase SSR client with Clerk
SupabaseSSupabase / help-and-questions
7mo ago
.rpc() with python supabase-client
SupabaseSSupabase / help-and-questions
4y ago