S
Supabase3y ago
Devr

Any way to access the access_token from session on server ? (Am using Sveltekit)

I cannot actually access the users access token on server through getsession() , When I pass it through authorization header it responds That invalid JWT and cant parse it due to incomplete segments, please suggest a way so that I can fetch a user or use CRUD through API on server, Thanks
No description
7 Replies
garyaustin
garyaustin3y ago
Not sure how you are getting the session info to the server from the client. https://github.com/supabase/auth-helpers is one way to deal with passing info to server code and has a sveltekit version. Also getSession will not work on a server (if you are somehow setting it) without persistSession flag being false in createClient call currently. It requires localstorage to work.
GitHub
GitHub - supabase/auth-helpers: A collection of framework specific ...
A collection of framework specific Auth utilities for working with Supabase. - GitHub - supabase/auth-helpers: A collection of framework specific Auth utilities for working with Supabase.
Devr
DevrOP3y ago
Then how would I pass acces_token to auth header ? Using API as shown in my image Sorry if this sounds silly but am still learning
garyaustin
garyaustin3y ago
I don't do server side stuff. There are discussions here and on github. The auth-helpers where designed to deal with getting the token to the server side of things (I think using cookies, but not sure). You might be able to do it on your own, but you have to have a variable or storage accessible on both sides to put it into.
j4
j43y ago
Auth helpers are great here. If you don't go that route, then you'd typically set a cookie with the token values after sign-in. Then pass those into event.locals, in your hooks.server.js file, so you can grab them on the server side. Also, you'd want to move your supabase request into the load function. However, if I'm reading your code correctly, if you're doing this to get info about the user from /v1/user, you can make a supabase client request for getUser(access_token)
j4
j43y ago
I have a repo with some of this. https://github.com/j4w8n/sveltekit-supabase-oauth
GitHub
GitHub - j4w8n/sveltekit-supabase-oauth: Supabase OAuth, with Svelt...
Supabase OAuth, with SvelteKit. Contribute to j4w8n/sveltekit-supabase-oauth development by creating an account on GitHub.
aleks_maker
aleks_maker3y ago
Hey, If you don't want to reimplement subabase-js SDK then use it, it will take token from the cookie and add to your request. The easiest way to communicate to Supabase from JS. Here is the solid.js example to get the idea where is session came from. There is an access_token on the top on the session, if you need it. Hope it helps.
No description
Devr
DevrOP3y ago
Oh thanks , that repo helps! Yes, I was just thinking if it works without any packages, but will use auth helpers , thanks!

Did you find this page helpful?