diff options
Diffstat (limited to 'fe/src/lib/LoginForm.svelte')
-rw-r--r-- | fe/src/lib/LoginForm.svelte | 105 |
1 files changed, 62 insertions, 43 deletions
diff --git a/fe/src/lib/LoginForm.svelte b/fe/src/lib/LoginForm.svelte index 499a457..bf6d9ad 100644 --- a/fe/src/lib/LoginForm.svelte +++ b/fe/src/lib/LoginForm.svelte | |||
@@ -1,66 +1,85 @@ | |||
1 | <script lang='ts'> | 1 | <script lang="ts"> |
2 | import { token, user, preferences } from '../stores/auth'; | 2 | import { token, user, preferences } from "../stores/auth"; |
3 | import Card from './Card.svelte'; | 3 | import Card from "./Card.svelte"; |
4 | 4 | ||
5 | let credentials: CredentialObject = { | 5 | let credentials: CredentialObject = { |
6 | username: '', | 6 | username: "", |
7 | password: '' | 7 | password: "", |
8 | } | 8 | }; |
9 | 9 | ||
10 | let error; | 10 | let error: string | null = null; |
11 | 11 | ||
12 | interface CredentialObject { | 12 | interface CredentialObject { |
13 | username: string; | 13 | username: string; |
14 | password: string; | 14 | password: string; |
15 | } | 15 | } |
16 | 16 | ||
17 | function prepareCredentials ({ username, password }: CredentialObject): string { | 17 | function prepareCredentials({ |
18 | username, | ||
19 | password, | ||
20 | }: CredentialObject): string { | ||
18 | return btoa(`${username}:${password}`); | 21 | return btoa(`${username}:${password}`); |
19 | } | 22 | } |
20 | 23 | ||
21 | async function onSubmit (e) { | 24 | async function onSubmit(e: Event) { |
22 | if (!credentials.username || !credentials.password) { | 25 | if (!credentials.username || !credentials.password) { |
23 | error = 'please enter your username and password'; | 26 | error = "please enter your username and password"; |
24 | return; | 27 | return; |
25 | } | 28 | } |
26 | const auth = prepareCredentials(credentials); | 29 | const auth = prepareCredentials(credentials); |
27 | 30 | ||
28 | const response = await fetch('http://localhost:8080/api/v1/auth', { | 31 | const response = await fetch("http://localhost:8080/api/v1/auth", { |
29 | method: 'POST', | 32 | method: "POST", |
30 | headers: { | 33 | headers: { |
31 | 'Authorization': `Basic ${auth}`, | 34 | Authorization: `Basic ${auth}`, |
32 | }, | 35 | }, |
33 | }); | 36 | }); |
34 | 37 | ||
35 | if (response.status === 401) { | 38 | if (response.status === 401) { |
36 | error = "Your username or password is wrong"; | 39 | error = "Your username or password is wrong"; |
37 | return; | 40 | return; |
38 | } | 41 | } |
39 | 42 | ||
40 | if (response.ok) { | 43 | if (response.ok) { |
41 | const { token: apiToken, user: userData, preferences: userPreferences } = await response.json(); | 44 | const { |
42 | user.setUser(userData); | 45 | token: apiToken, |
43 | preferences.set(userPreferences); | 46 | user: userData, |
44 | token.authenticate(apiToken); | 47 | preferences: userPreferences, |
48 | } = await response.json(); | ||
49 | user.setUser(userData); | ||
50 | preferences.set(userPreferences); | ||
51 | token.authenticate(apiToken); | ||
45 | } | 52 | } |
46 | 53 | ||
47 | error = null; | 54 | error = null; |
48 | } | 55 | } |
49 | </script> | 56 | </script> |
50 | 57 | ||
51 | <Card> | 58 | <Card> |
52 | <form class="form" on:submit|preventDefault={onSubmit}> | 59 | <form class="form" on:submit|preventDefault={onSubmit}> |
53 | <div class='form input group'> | 60 | <div class="form input group"> |
54 | <label for="username">Username</label> | 61 | <label for="username">Username</label> |
55 | <input bind:value={credentials.username} id="username" name='username' type="text" autocomplete="username" /> | 62 | <input |
56 | </div> | 63 | bind:value={credentials.username} |
57 | <div class='form input group'> | 64 | id="username" |
58 | <label for="password">Password</label> | 65 | name="username" |
59 | <input bind:value={credentials.password} id="password" name='password' type="password" autocomplete="current-password"/> | 66 | type="text" |
60 | </div> | 67 | autocomplete="username" |
61 | {#if error} | 68 | /> |
62 | <p class="error">{error}</p> | 69 | </div> |
63 | {/if} | 70 | <div class="form input group"> |
64 | <button type="submit">Log in</button> | 71 | <label for="password">Password</label> |
65 | </form> | 72 | <input |
73 | bind:value={credentials.password} | ||
74 | id="password" | ||
75 | name="password" | ||
76 | type="password" | ||
77 | autocomplete="current-password" | ||
78 | /> | ||
79 | </div> | ||
80 | {#if error} | ||
81 | <p class="error">{error}</p> | ||
82 | {/if} | ||
83 | <button type="submit">Log in</button> | ||
84 | </form> | ||
66 | </Card> | 85 | </Card> |