I've been struggling with Supabase user's being nu...
# help
m
I've been struggling with Supabase user's being null on the server but not on the client. On the server I'm using
supabase.auth.api.getUserByCookie(req)
(using next.js), and on the client I'm using
supabase.auth.user()
. What often happens is in a pretty short time period after authentication (30 mins or so), the
getUserByCookie
starts returning null, but
supabase.auth.user()
correctly resolves the user on the client. I still have the sb:token cookie and it's not expired yet, so I'm confused why that's not working Any idea what I might be doing wrong?
s
You need to set the cookie first before you can get the cookie
supabase.auth.api.setAuthCookie
m
It looks like am calling that following this API route from the next.js example: https://github.com/supabase/supabase/blob/master/examples/nextjs-with-supabase-auth/pages/api/auth.js
I can confirm I have the
sb:token
cookie set
Okay so I tried the workaround suggested in that issue where I call the supabase auth endpoint like this:
And I'm getting a 401 with the msg 'Invalid token: token is expired by 20m24s'
But the client seems totally happy with the token
s
Hmm I'm not sure about that workaround, unfortunately I'm not using Supabase with Next.js but with SvelteKit and the setup is sorta similar, but I'm definitely getting the user on the server as well as the client
I can share a link to one of my project that is open source if it helps
m
Sure, that could help! Thanks
s
Most of the server stuff happens in the handleAuth.js file https://github.com/silentworks/waiting-list/blob/main/src/lib/handleAuth.js
m
Thanks, and just found this issue that might be what I'm experiencing https://github.com/supabase/gotrue-js/issues/32#issuecomment-797012940
awesome will take a look!
s
I'm almost certain the auto refresh issue was fixed months ago
I wonder if there has been a regression of that issue
m
That or maybe the next.js example is missing something? I'm not using the
@supabase/ui
Auth
stuff, I'm guessing there's nothing in there that would be relevant for refreshing tokens. Only thing I can see in that source for that component that i'm not using is auth.session()
s
Yeah auth.session() shouldn't make a difference in this case
Btw is the nextjs with supabase auth example code not working for you too?
m
Not sure, I'll give that a try shortly
I never actually ran it, just pulled bits and pieces from it
r
I am also facing this issue with nextjs and supabase, after some inactivity or if i change some code while server running on localhost, the user becomes null, not sure why, in supabase settings the jwt expiry is 1 hr but this is happening very oftenly.
s
Can you create an issue on the Supabase GitHub along with the steps taken to get this result. Also if possible create an example repo that replicates the issue.
Please do remember however that it’s the holiday period and getting someone to look at the issue may take a bit longer than usual. But I’ll try my best to help where I can.