mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 02:53:50 +00:00
36 lines
958 B
TypeScript
36 lines
958 B
TypeScript
import { createBrowserClient, createServerClient, isBrowser } from '@supabase/ssr';
|
|
import { PUBLIC_SUPABASE_ANON_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public';
|
|
import type { LayoutLoad } from './$types';
|
|
|
|
export const load: LayoutLoad = async ({ fetch, data, depends }) => {
|
|
depends('supabase:auth');
|
|
|
|
const supabase = isBrowser()
|
|
? createBrowserClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY, {
|
|
global: {
|
|
fetch
|
|
}
|
|
})
|
|
: createServerClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY, {
|
|
global: {
|
|
fetch
|
|
},
|
|
cookies: {
|
|
getAll() {
|
|
return data.cookies;
|
|
}
|
|
}
|
|
});
|
|
|
|
/**
|
|
* It's fine to use `getSession` here, because on the client, `getSession` is
|
|
* safe, and on the server, it reads `session` from the `LayoutData`, which
|
|
* safely checked the session using `safeGetSession`.
|
|
*/
|
|
const {
|
|
data: { session }
|
|
} = await supabase.auth.getSession();
|
|
|
|
return { supabase, session };
|
|
};
|