<script>
    import { onMount } from 'svelte';
    import { page } from '$app/stores';
    import { goto } from '$app/navigation';
    import { member, intros } from '../../store.ts';

    const code = $page.url.searchParams.get('code');

    let loginFailed = false;

    onMount(async () => {
        const response = await fetch(`http://localhost:7756/auth?code=${code}`);
        const body = await response.json();

        if (!response.ok)  {
            loginFailed = true
        } else {
            await member.login(body.token);
            await intros.fetchIntros($member.guilds);

            goto('/')            
        }
    });
</script>

{#if loginFailed}
    <p style='color: red'>Login failed</p>
{/if}
