supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

Passing localstorage is necessary while using supabase-js in a deno runtime

Open nohehf opened this issue 2 years ago • 7 comments

Improve documentation

Link

This page could be improved

Describe the problem

While using supabase-js using deno, I ran into an issue: the client was not saving the session. I figured out this comes from the client meant to be used in a browser and then using local storage. Indeed manually passing the storage while initializing the client fixed the issue:

const supabase = createClient(env.API_URL, env.SUPABASE_KEY, {
  auth: {
    storage: localStorage,
  },
});

I struggled to find the solution and had to test things based on function signatures, and other people would like to be able to use the client on a server.

Describe the improvement

The above page should mention this and provide a solution for Deno and nodejs. Tho for nodejs a third-party package might be necessary.

Related issues

I believe supabase/gotrue-js#771 is related to the same issue, tho with nodejs.

nohehf avatar Jan 09 '23 23:01 nohehf

You should also be able to set the client's auth > persistSession option as false. This will cause the session to be stored in memory.

Either way, you are correct - it needs some documentation.

j4w8n avatar Jan 10 '23 02:01 j4w8n

Yes, indeed, and this is a particularly useful (and probably widely needed) use-case for edge functions performing actions on the DB.

nohehf avatar Jan 11 '23 19:01 nohehf

You should also be able to set the client's auth > persistSession option as false. This will cause the session to be stored in memory.

Either way, you are correct - it needs some documentation.

Setting persistSession to false doesn't work. It is still not creating the in-memory session.

antick avatar Feb 05 '23 06:02 antick

You should also be able to set the client's auth > persistSession option as false. This will cause the session to be stored in memory.

Either way, you are correct - it needs some documentation.

Setting persistSession to false doesn't work. It is still not creating the in-memory session.

What makes you say this? Unless code has recently changed, it's one or the other. If true, it attempts to use local storage; if false, it uses memory via variables in the client.

j4w8n avatar Feb 05 '23 12:02 j4w8n

You should also be able to set the client's auth > persistSession option as false. This will cause the session to be stored in memory. Either way, you are correct - it needs some documentation.

Setting persistSession to false doesn't work. It is still not creating the in-memory session.

What makes you say this? Unless code has recently changed, it's one or the other. If true, it attempts to use local storage; if false, it uses memory via variables in the client.

Hey, I have been struggling with this problem for hours and setting that flag to false alone didn't make any difference.

This worked for me though:

this.clientInstance = createClient(
  this.configService.get('SUPABASE_URL'),
  this.configService.get('SUPABASE_KEY'),
  {
    global: {
      headers: {
        Authorization: this.request.headers.authorization,
      }
    }
  }
);

Update:

The discussion below helped me a lot to understand the problem. This also has few potential solutions, though not as easy as using the setAuth but at least gets the job done.

https://github.com/supabase/gotrue-js/pull/340#issuecomment-1260942393

I really hope supabase team updates the documentation with these changes and examples properly. Figuring all this out by myself was very counter productive while I should have been able to find this in the documentation easily.

antick avatar Feb 05 '23 12:02 antick

Glad you got it working.

There are some links to examples, which talk about passing global headers.

https://supabase.com/docs/guides/functions 👇 https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts#L26

j4w8n avatar Feb 05 '23 13:02 j4w8n

For Cloudflare Workers, this was the solution:

const supabase_client = createClient(c.env.SUPABASE_URL, c.env.SUPABASE_KEY, {
  auth: {
    persistSession: false
  }
})

Thanks to https://github.com/supabase/supabase-js/issues/684#issuecomment-1376620014 for explaining it! (Just writing this comment so that I and others will find it indexed on Google in the future.)

Manouchehri avatar May 01 '23 05:05 Manouchehri