SupabaseS
Supabase4y ago
j4

SvelteKit Auth Helper

I'm curious why onAuthStateChange() is placed inside onMount(). Anyone have insight? Not trying to imply it shouldn't, just want to better understand.

<!-- src/routes/+layout.svelte -->
<script>
  import { supabaseClient } from '$lib/db';
  import { invalidate } from '$app/navigation';
  import { onMount } from 'svelte';

  onMount(() => {
    const {
      data: { subscription }
    } = supabaseClient.auth.onAuthStateChange(() => {
      invalidate('supabase:auth');
    });

    return () => {
      subscription.unsubscribe();
    };
  });
</script>

<slot />
Was this page helpful?