does anyone know why `auth.user()` doesn't work in...
# javascript
b
does anyone know why
auth.user()
doesn't work in the
load()
function in sveltekit? it always returns
null
, even though it works fine in the regular script tags. in the load function, i'm trying to query from a table with a rls policy
auth.role() = 'authenticated
, but it keeps failing. changing the check with
auth.role() = 'anon'
works for some reason. i'm guessing this is related to the
auth.user()
returning
null
.
c
Yep, this is most likely the reason. I have an explanation about why I think this happens here: https://github.com/supabase/gotrue-js/issues/143#issuecomment-970288835
The workaround is to introduce 3 states for your user: (1) unauthenticated (2) pending authentication (3) authenticated
v
I don't know sveltekit very well, but the auth.user() function works only on the client.
Because its stored in localStorage
c
aaaa, good point @User - the explanation I linked is actually about the client
v
is the load() function on the server?
b
it runs on both the server & the client
v
I guess its not made for fetching data then? That would be inefficient! What do you use it for?
b
i'm using it to get the data from supabase before the component loads
v
Would that not load it twice?
b
i'm not sure myself
v
Or is it one function that does not stop loading?
b
it's like getServerSideProps from nextjs
v
But that only runs on the server?
b
in sveltekit it runs on the client too. i got this from the docs, but not sure as to how it actually works
v
But then you just load the resources from apis or whatever twice, don't you?
b
yes probablty
v
Thats wierd.