aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/login.svelte
blob: 09b1834e3902100f4bd916cf3068465a6c7eec9d (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
38
39
40
41
42
43
44
45
46
<script lang="ts">
  import { get_session_token, set_session_token } from "$lib/auth";
  import { goto } from "$app/navigation";

  let username: string | undefined;
  let password: string | undefined;

  const onSubmit = () => {
    fetch("/api/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        username,
        password,
      }),
    })
      .then((response) => {
        if (!response.ok) {
          throw Error(response.statusText);
        }
        return response.text();
      })
      .then((token) => {
        set_session_token(token);
        goto("/");
      });
  };

  function loggedIn(): boolean {
    return get_session_token() != null;
  }
</script>

{#if loggedIn()}
  you are logged in
{/if}

<form on:submit|preventDefault={onSubmit}>
  <label for="username">Username</label>
  <input name="username" bind:value={username} />
  <label for="password">Password</label>
  <input type="password" name="password" bind:value={password} />
  <button type="submit">Log in</button>
</form>