Unable to run UPDATE query on auth.users.raw_user_meta_data

Hello, I am currently facing an issue where I am unable to modify the raw_user_meta_data column of the auth.users table.

I am using a function (security definer, returns trigger, no params) on the public schema with a trigger (auth.users table, after insert, for each row) on the auth schema.

The function code is below:
declare --
new_user_data jsonb;
username text;
bio text;
avatar text;
complete_user_data jsonb;
BEGIN --
IF new.raw_app_meta_data->>'provider' != 'email' THEN -- 
-- Set the variables
username := new.raw_user_meta_data->>'name';
bio := concat(
    'Hi, I am ',
    new.raw_user_meta_data->>'name',
    '!'
);
avatar := new.raw_user_meta_data->>'picture';
-- Set the fields in the new_user_data
new_user_data := jsonb_build_object(
    'username',
    username,
    'bio',
    bio,
    'avatar',
    avatar
);
END IF;
-- Combine the new_user_data with the existing raw_user_meta_data
complete_user_data := (new.raw_user_meta_data || new_user_data);
-- Log the data
RAISE WARNING 'complete_user_data: %',
complete_user_data::text::jsonb;
-- Update the raw_user_meta_data
UPDATE auth.users
SET raw_user_meta_data = complete_user_data::text::jsonb
WHERE id = new.id;
-- insert the new user into the "User" table
INSERT INTO public."User" (id, email, username, bio, avatar)
VALUES (
        new.id,
        new.email,
        complete_user_data->>'username',
        complete_user_data->>'bio',
        complete_user_data->>'avatar'
    );
-- Return the new row
RETURN new;
END;
Was this page helpful?