SupabaseS
Supabase•4y ago
CanRau

Helper postgres function `safe_auth_json`

I was trying to make a helper function to convert a
auth.users
row to json stripping away sensitive data.

Using just row_to_json within the trigger function works perfectly fine.

It would just make things "cleaner" and I'd especially like to understand why I don't get it working 😅

Following a few versions I tried, probably not all versions, though all error trying them in the Dashboards SQL Editor

CREATE FUNCTION safe_json(_row json)
  RETURNS json
AS $$
  BEGIN
  RETURN row_to_json(_row)::jsonb-ARRAY['encrypted_password', 'confirmation_token', 'recovery_token', 'phone_change_token', 'email_change_token_new', 'reauthentication_token', 'email_change_token_current'];
  END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION safe_auth_json(_row auth.users)
  RETURNS json
AS $$
  SELECT row_to_json(_row)::jsonb-ARRAY['encrypted_password', 'confirmation_token', 'recovery_token', 'phone_change_token', 'email_change_token_new', 'reauthentication_token', 'email_change_token_current'];
$$ LANGUAGE sql;


CREATE OR REPLACE FUNCTION safe_auth_json(_row auth.users)
  RETURNS json
AS $$
  SELECT row_to_json(*)::jsonb-ARRAY['encrypted_password', 'confirmation_token', 'recovery_token', 'phone_change_token', 'email_change_token_new', 'reauthentication_token', 'email_change_token_current'] FROM _row;
$$ LANGUAGE sql;


CREATE OR REPLACE FUNCTION safe_auth_json(_row auth.users)
  RETURNS json
AS $$
  SELECT row_to_json(_row)::jsonb-ARRAY['encrypted_password', 'confirmation_token', 'recovery_token', 'phone_change_token', 'email_change_token_new', 'reauthentication_token', 'email_change_token_current'];
$$ LANGUAGE sql;
Was this page helpful?