1

I am getting error calling

supabase.storage
          .from('public')
          .upload(file.name, file)
          .then(({ error }) => {
            console.log('upload error', error)
            if (error) {
              snackbar.error(
                'There was an error uploading the file, please try again later',
              )
              return
            }
          })

and got

Object { statusCode: "500", error: "Database Error", message: 'invalid input syntax for type uuid: "user_2OHvEF7KJkNHo7fIirckoCSf9D4"' }

looks like supabase storage is going through a different auth that reads sub field in the jwt token, I tried remove all the RLS in storage and the problem still exist. Is this something solvable on our side?

2
  • Supabase storage reads the user id from the JWT to insert it as the owner of the object whenever you upload something. Could you share the piece of code where you are setting the auth headers? Commented May 3, 2023 at 6:22
  • @dshukertjr I think the problem is at its integration with clerk, clerk uses non uuid for their userId which happens to be the sub field in the claim Commented May 4, 2023 at 14:06

1 Answer 1

0

First, you'll need to drop the FK from the storage.objects table:

ALTER TABLE storage.objects 
DROP CONSTRAINT objects_owner_fkey;

Then, you can use these functions to format anything into UUID:

CREATE OR REPLACE FUNCTION convert_to_uuid(input_value text)
RETURNS uuid AS $$
DECLARE
  hash_hex text;
BEGIN
  -- Return null if input_value is null or an empty string
  IF input_value IS NULL OR NULLIF(input_value, '') IS NULL THEN
    RETURN NULL;
  END IF;
  hash_hex := substring(encode(digest(input_value::bytea, 'sha512'), 'hex'), 1, 36);
  RETURN (left(hash_hex, 8) || '-' || right(hash_hex, 4) || '-4' || right(hash_hex, 3) || '-a' || right(hash_hex, 3) || '-' || right(hash_hex, 12))::uuid;
END;
$$ LANGUAGE plpgsql IMMUTABLE;

For Clerk specifically, you can alter their suggested function with the following:

CREATE OR REPLACE FUNCTION requesting_user_id()
RETURNS uuid
LANGUAGE SQL STABLE
AS $$
  select convert_to_uuid(current_setting('request.jwt.claims', true)::json->>'sub');
$$;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this but how to change the user_id send to storage ? I still have the error. It's working for Public DB but not storage... Thanks.
Maybe its a function executed in the storage database but they are not editable... "invalid input syntax for type uuid: \"user_2XcPnlxS6EM3eepbCwhQsZ8ViDP\""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.