<script>
    import { onMount } from 'svelte';
    import { member, intros } from './store.ts';

    const authorizeUri = "https://discord.com/api/oauth2/authorize?client_id=577634620728934400&redirect_uri=http%3A%2F%2Flocalhost%3A5173%2Fauth&response_type=code&scope=identify%20guilds%20guilds.members.read";

    onMount(async () => {
        const token = window.localStorage.getItem('token');

        if (!!token) {
            await member.pullData(token);
            await intros.fetchIntros($member.guilds);
        }
    }); 

    const login = async (username) => {
        window.location = authorizeUri;
    }

    let loginPromise = null;

    let enteredUsername = '';
</script>

<p style="color:red;">You need to login first</p>
<button on:click={() => loginPromise = login(enteredUsername)}>Login</button>
