summaryrefslogtreecommitdiff
path: root/ui/lib/components/LogIn.svelte
blob: e1cda8a42b68d71fb2a5f45161864b5e42b1b16d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<script>
    import { goto } from '$app/navigation';
    import { logIn } from '$lib/apiServer';
    import { currentUser } from '$lib/store';

    let disabled = false;
    let username = '';
    let password = '';

    async function handleLogin() {
        disabled = true;
        const response = await logIn(username, password);
        if (200 <= response.status && response.status < 300) {
            currentUser.update(() => ({ username }));
            username = '';
            password = '';
            goto('/');
        }
        disabled = false;
    }
</script>

<div class="card m-4 p-4">
    <form on:submit|preventDefault={handleLogin}>
        <label class="label" for="username">
            username
            <input class="input" name="username" type="text" placeholder="username" bind:value={username} disabled={disabled}>
        </label>
        <label class="label" for="password">
            password
            <input class="input" name="password" type="password" placeholder="password" bind:value={password} disabled={disabled}>
        </label>
        <button class="btn variant-filled" type="submit">
            sign in or up
        </button>
    </form>
</div>