I have a Child component where I check if user is logged in:
<script>
import { goto } from '$app/navigation';
import { user } from '../sessionStore';
import { supabase } from '../supabaseClient';
import Dashboard from './dashboard.svelte';
import Signin from './signin.svelte';
let loading = false;
async function isLogged() {
try {
loading = true;
const user = supabase.auth.user();
supabase.auth.onAuthStateChange((_, session) => {
user.set(session.user);
});
let { data, error, status } = await supabase
.from('users')
.select('*')
.eq('id', user.id)
.single();
if (error && status !== 406) throw error;
if (data) {
goto('/signin');
}
} catch (error) {
goto('/signin');
} finally {
loading = false;
}
}
<p use:isLogged></p>
If I go to this page directly it works but when I call this component in my other pages its not working, in my dashboard I call this component simply by importing it and inserting it in the page:
<Child/>
How can I make this work? Please let me know!