auth-helpers-sveltekit best way to access session

Hey peeps, I am making a fresh sveltekit project with supabase for db and auth and just picking up from the documentation example. I noticed 2 ways of accessing the session inside a load function:
const { session} = await getSupabase(event)
const { session} = await getSupabase(event)
and
const session = await getServerSession(event)
const session = await getServerSession(event)
and I was wondering what's the difference between the 2 and whether I should prefer one over the other (e.g. one for server side and one for client side?). Also do I have to load it like that on every route? Can't I just load it on my root's +layout.server.ts and pass it down through data and const { session } = await parent() in my subroute layouts?
3 Replies
j4
j43y ago
I don't use the helper, so maybe someone can confirm or deny. Yeah, use getServerSession in your server load functions. If you want to use invalidate('supabase:auth') to help keep the session data synced, then you have to use getSupabase in your client load functions. I'm not even sure if returning the session is necessary in this case, if you'd rather use await parent().
darkness_2
darkness_23y ago
^ I was wondering the above as well. In the @supabase/auth-helpers-sveltekit docs, it recommends creating both a +layout.server.ts and a +layout.ts that both return session. I imagine in that case, you don't actually need the .server version as the shared version will always override it?
j4
j43y ago
I believe so??

Did you find this page helpful?