nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.16k stars 3.35k forks source link

Supabase adapter with supabase storage error possible workaround #7452

Open Zhumatic opened 1 year ago

Zhumatic commented 1 year ago

What is the improvement or update you wish to see?

  1. Create client accroding to the supabase adapter docs like this:
    
    const { supabaseAccessToken } = session;

const supabase = createClient( process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, { global: { headers: { Authorization: Bearer ${supabaseAccessToken}, }, }, } );

2. Then using this client to perform supabase storage upload will result in error: parent resource cannot be found. Supabase postgres log shows this error actually is: 

insert or update on table "objects" violates foreign key constraint "objects_owner_fkey

If you look at the storage objects table definition will find following

constraint objects_owner_fkey foreign key (owner) references auth.users (id)


3. Possible cause of the problem: next_auth schema is created and used in order to implement next auth instead of the default auth schema.

4. Possible workaround: 

alter table storage.objects drop constraint objects_owner_fkey, add constraint objects_owner_fkey foreign key (owner) references next_auth.users(id)


after this sql action, above supabase client works with storage upload, and next_auth.user(id) is loaded into the "owner" column of the objects table upon upload action.

5. Is it a good idea to mention the inconsistency and possible official solution in the supabase adapter documentation?

### Is there any context that might help us understand?

related issue from supabase:
"Storage.objects table should not use fk constraint with owners column on auth.users id column "
https://github.com/supabase/storage-api/issues/276

### Does the docs page already exist? Please link to it.

https://authjs.dev/reference/adapter/supabase
github-actions[bot] commented 1 month ago

This issue was marked with the help needed label by a maintainer.

The issue might require some digging, so it is recommended to have some experience with the project.

Have a look at the Contributing Guide first.

This will help you set up your development environment to get started. When you are ready, open a PR, and link back to this issue in the form of adding Fixes #1234 to the PR description, where 1234 is the issue number. This will auto-close the issue when the PR gets merged, making it easier for us to keep track of what has been fixed.

Please make sure that - if applicable - you add tests for the changes you make.

If you have any questions, feel free to ask in the comments below or the PR. Generally, you don't need to @mention anyone directly, as we will get notified anyway and will respond as soon as we can)

[!NOTE]
There is no need to ask for permission "can I work on this?" Please, go ahead if there is no linked PR :slightly_smiling_face: