aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/login.svelte
blob: 911635b4d0035648273ed6b0368641f64a3dde7f (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
47
48
49
50
51
52
53
54
55
<script lang="ts">
  import { get_session_token, set_session_token } from "$lib/auth";
  import { goto } from "$app/navigation";
  import { currentUser } from "$lib/stores/current_user";

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

  async function login() {
    let response = await fetch("/api/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        username,
        password,
      }),
    });

    if (!response.ok) {
      throw Error(response.statusText);
    }

    let token = response.headers.get("Token");
    set_session_token(token);

    const user = await response.json();
    currentUser.set(user);

    goto("/");
  }

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

<div class="page-card">
  <div class="page-card-content">
    <h1 class="page-card-header">Sign in</h1>
    <form class="account-form" on:submit|preventDefault={login}>
      <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">Submit</button>
    </form>
  </div>
</div>

<style lang="scss">
  @import "src/styles/account_forms.scss";
</style>