supabase / auth-helpers

A collection of framework specific Auth utilities for working with Supabase.
https://supabase.github.io/auth-helpers/
MIT License
902 stars 237 forks source link

Cannot destructure property 'supabaseUrl' of 'getConfig(...)' as it is undefined. #385

Closed angezanetti closed 1 year ago

angezanetti commented 1 year ago

Bug report

Describe the bug

I followed the @supabase/auth-helpers-sveltekit guide on how to integrate supabase-auth helpers with sveltekit.

It works, but when I stop my server and start it again with npm run dev I got the error:

Cannot destructure property 'supabaseUrl' of 'getConfig(...)' as it is undefined.
TypeError: Cannot destructure property 'supabaseUrl' of 'getConfig(...)' as it is undefined.
    at getRequestSupabaseClient (file:///Users/xavier/Projects/remoteFr2/node_modules/@supabase/auth-helpers-sveltekit/dist/utils/supabase-request.js:10:13)
    at Proxy.getServerSession (file:///Users/xavier/Projects/remoteFr2/node_modules/@supabase/auth-helpers-sveltekit/dist/utils/getServerSession.js:3:22)
    at load (/src/routes/+layout.server.js:5:19)
    at load_server_data (file:///Users/xavier/Projects/remoteFr2/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:33:41)
    at respond_with_error (file:///Users/xavier/Projects/remoteFr2/node_modules/@sveltejs/kit/src/runtime/server/page/respond_with_error.js:41:32)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async resolve (file:///Users/xavier/Projects/remoteFr2/node_modules/@sveltejs/kit/src/runtime/server/index.js:261:12)
    at async respond (file:///Users/xavier/Projects/remoteFr2/node_modules/@sveltejs/kit/src/runtime/server/index.js:294:20)
    at async file:///Users/xavier/Projects/remoteFr2/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:397:22

I checked the issue #310, but I import the right package in $lib/db and I do have the hooks.client.js and the hooks.server.js witht the import $lib/db

To Reproduce

Follow the guide, and restart your local server

Expected behavior

It should handle the sessions

Screenshots

System information

silentworks commented 1 year ago

What version of the auth helper are your using? I can't replicate this issue in the latest version.

david-plugge commented 1 year ago

My guess is that you forgot to import $lib/db inside both hooks files hooks.client.js and hooks.server.js. We probably want to throw an error with a better error message if we call getConfig internally but createClient wasnt called before.

Edit:

Sorry, should have completely read through the issue first

david-plugge commented 1 year ago

But i´m not able to reproduce the issue aswell. The only thing i noticed is that i had to restart the dev server when i created hooks.client.js but that´s a sveltekit issue.

Could you share a reproduction?

wozzashek commented 1 year ago

I get this issue when trying to follow this in StackBlitz. Not seen it when running the tutorial on my local machine.

So not sure if this is a StackBlitz

See: https://stackblitz.com/edit/sveltejs-kit-template-default-jrbksc?file=package.json

You will need to add your own .env file

david-plugge commented 1 year ago

Take a look at this section: https://supabase.com/docs/guides/auth/auth-helpers/sveltekit#set-up-the-supabase-client

You need to import lib/db in both hooks files to actually load the client

Edit:

Oh, you created both hooks files. But they need to be located in /src

wozzashek commented 1 year ago

Thanks @david-plugge you spotted my issue. I wonder if original poster also made the same mistake I did, having hooks files outside src folder.

sjce-development commented 1 year ago

Thanks a lot too, my issue was having the hook files inside of routes instead of the /src folder, this really helped a lot

Olyno commented 1 year ago

Hi 👋🏻 Having the same issue right now, here is my reproduction repository: https://github.com/Olyno/supabase-svelte-helper-auth-repro

This issue happens only on the http://localhost:3000/auh page for some reasons.

david-plugge commented 1 year ago

Hi @Olyno, I think you forgot to push some changes, the repo shows a plain sveltekit app without supabase installed

Olyno commented 1 year ago

Oh nice catch, i pushed it on the wrong branch 😅 fixed!

david-plugge commented 1 year ago

Alright, it seems like you are missing a hooks.client.ts file with the import from $lib/db. The import is necessary because the helpers must be initialized before any load functions run

Olyno commented 1 year ago

Indeed, the hooks.client.ts file was missing, it solves the issue, thank you! May i ask why do we need 2 kind of hooks to use the helper?

ReeceLangerock commented 1 year ago

I had the same error, and followed the suggestions from this issue and issue #310 and nothing was helping. I ended up wrapping await getSupabase(event) from +layout.ts in a try catch block and that "fixed" it. It seems that on initial load of the vite sveltekit server getLoadSupabaseClient is being called before setConfig.

silentworks commented 1 year ago

Code is outdated and no longer used.