supabase-js
supabase-js copied to clipboard
Passing localstorage is necessary while using supabase-js in a deno runtime
Improve documentation
Link
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.
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.
Yes, indeed, and this is a particularly useful (and probably widely needed) use-case for edge functions performing actions on the DB.
You should also be able to set the client's
auth
>persistSession
option asfalse
. 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.
You should also be able to set the client's
auth
>persistSession
option asfalse
. 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.
You should also be able to set the client's
auth
>persistSession
option asfalse
. 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; iffalse
, 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.
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
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.)