feat(#14): Update code to match documentation

This commit is contained in:
Josh Creek
2024-07-04 19:27:52 +01:00
parent e15565bb56
commit 113268614b
6 changed files with 42 additions and 38 deletions
+3 -2
View File
@@ -1,10 +1,11 @@
import type { LayoutServerLoad } from './$types';
export const load: LayoutServerLoad = async ({ locals: { safeGetSession } }) => {
export const load: LayoutServerLoad = async ({ locals: { safeGetSession }, cookies }) => {
const { session, user } = await safeGetSession();
return {
session,
user
user,
cookies: cookies.getAll()
};
};
+2 -2
View File
@@ -32,8 +32,8 @@
let cookiesAccepted = false;
onMount(() => {
const { data } = supabase.auth.onAuthStateChange((event, _session) => {
if (_session?.expires_at !== session?.expires_at) {
const { data } = supabase.auth.onAuthStateChange((event, newSession) => {
if (newSession?.expires_at !== session?.expires_at) {
// tells SvelteKit that the root +layout.ts load function should be executed whenever the session updates to keep the page store in sync.
invalidate('supabase:auth');
}
+16 -15
View File
@@ -1,25 +1,26 @@
import { createBrowserClient, createServerClient, isBrowser } from '@supabase/ssr';
import { PUBLIC_SUPABASE_ANON_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public';
import type { LayoutLoad } from './$types';
import { createBrowserClient, isBrowser, parse } from '@supabase/ssr';
export const load: LayoutLoad = async ({ fetch, data, depends }) => {
depends('supabase:auth');
const supabase = createBrowserClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY, {
global: {
fetch
},
cookies: {
get(key) {
if (!isBrowser()) {
return JSON.stringify(data.session);
const supabase = isBrowser()
? createBrowserClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY, {
global: {
fetch
}
const cookie = parse(document.cookie);
return cookie[key];
}
}
});
})
: 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
-2
View File
@@ -1,6 +1,4 @@
<script lang="ts">
import { onDestroy } from 'svelte';
import { type User } from '@supabase/auth-js';
import SignUp from '$lib/components/SignUp.svelte';
export let data;
+1
View File
@@ -0,0 +1 @@
<p>Login error</p>