Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update sveltekit auth helpers guide

Andrew Smith694621f50abb
diff --git a/apps/docs/pages/guides/auth/auth-helpers/sveltekit.mdx b/apps/docs/pages/guides/auth/auth-helpers/sveltekit.mdx
index 6088372f64d..9197b29d33e 100644
--- a/apps/docs/pages/guides/auth/auth-helpers/sveltekit.mdx
+++ b/apps/docs/pages/guides/auth/auth-helpers/sveltekit.mdx
@@ -91,13 +91,12 @@ export const load: LayoutServerLoad = async ({ locals: { getSession } }) => {
};
```
-#### Shared Load functions and pages
+### Shared Load functions and pages
To be able to use Supabase in shared load functions and inside pages you need to create a Supabase client in the root layout load:
```ts
// src/routes/+layout.ts
-import { invalidate } from '$app/navigation';
import {
PUBLIC_SUPABASE_ANON_KEY,
PUBLIC_SUPABASE_URL
@@ -113,10 +112,7 @@ export const load: LayoutLoad = async ({ fetch, data, depends }) => {
supabaseUrl: PUBLIC_SUPABASE_URL,
supabaseKey: PUBLIC_SUPABASE_ANON_KEY,
event: { fetch },
- serverSession: data.session,
- onAuthStateChange() {
- invalidate('supabase:auth');
- }
+ serverSession: data.session
});
const {
@@ -129,10 +125,41 @@ export const load: LayoutLoad = async ({ fetch, data, depends }) => {
Access the client inside pages by `$page.data.supabase` or `data.supabase` when using `export let data: PageData`.
-The usage of `depends` and `invalidate` tells sveltekit that this load function should be executed whenever the session updates to keep the page store in sync.
+The usage of `depends` tells sveltekit that this load function should be executed whenever `invalidate` is called to keep the page store in sync.
`createSupabaseLoadClient` caches the client when running in a browser environment and therefore does not create a new client for every time the load function runs.
+### Setting up the event listener on the client side
+
+We need to create an event listener in the root `+layout.svelte` file in order catch supabase events being triggered.
+
+```svelte
+<!-- src/routes/+layout.svelte -->
+<script>
+ import { invalidate } from '$app/navigation';
+ import { onMount } from 'svelte';
+ import type { PageData } from './$types';
+
+ export let data: PageData;
+
+ $: ({ supabase } = data);
+
+ onMount(() => {
+ const {
+ data: { subscription },
+ } = supabase.auth.onAuthStateChange(() => {
+ invalidate('supabase:auth');
+ });
+
+ return () => subscription.unsubscribe();
+ });
+</script>
+
+<slot />
+```
+
+The usage of `invalidate` tells sveltekit that the root `+layout.ts` load function should be executed whenever the session updates to keep the page store in sync.
+
### Generate types from your database
In order to get the most out of TypeScript and it's intellisense, you should import the generated Database types into the `app.d.ts` type definition file that comes with your SvelteKit project, where `import('./DatabaseDefinitions')` points to the generated types file outlined in [v2 docs here](https://supabase.com/docs/reference/javascript/release-notes#typescript-support) after you have logged in, linked, and generated types through the Supabase CLI.
@@ -461,7 +488,7 @@ export const handle: Handle = async ({ event, resolve }) => {
### Initialize the client [#migration-initialize-client]
-In order to use the Supabase library in your client code you will need to setup a shared load function inside the root `+layout.ts` file.
+In order to use the Supabase library in your client code you will need to setup a shared load function inside the root `+layout.ts` and create a `+layout.svelte` to handle our event listening for Auth events.
<Tabs
scrollable
@@ -515,10 +542,7 @@ export const load: LayoutLoad = async ({ fetch, data, depends }) => {
supabaseUrl: PUBLIC_SUPABASE_URL,
supabaseKey: PUBLIC_SUPABASE_ANON_KEY,
event: { fetch },
- serverSession: data.session,
- onAuthStateChange() {
- invalidate('supabase:auth');
- }
+ serverSession: data.session
});
const {
@@ -529,6 +553,31 @@ export const load: LayoutLoad = async ({ fetch, data, depends }) => {
};
```
+```svelte title=src/routes/+layout.svelte
+<!-- src/routes/+layout.svelte -->
+<script>
+ import { invalidate } from '$app/navigation';
+ import { onMount } from 'svelte';
+ import type { PageData } from './$types';
+
+ export let data: PageData;
+
+ $: ({ supabase } = data);
+
+ onMount(() => {
+ const {
+ data: { subscription },
+ } = supabase.auth.onAuthStateChange(() => {
+ invalidate('supabase:auth');
+ });
+
+ return () => subscription.unsubscribe();
+ });
+</script>
+
+<slot />
+```
+
</TabPanel>
</Tabs>