diff options
Diffstat (limited to 'web/pw-server/src')
-rw-r--r-- | web/pw-server/src/lib/auth.ts | 7 | ||||
-rw-r--r-- | web/pw-server/src/routes/login.svelte | 46 | ||||
-rw-r--r-- | web/pw-server/src/routes/register.svelte | 38 |
3 files changed, 91 insertions, 0 deletions
diff --git a/web/pw-server/src/lib/auth.ts b/web/pw-server/src/lib/auth.ts new file mode 100644 index 0000000..436ec66 --- /dev/null +++ b/web/pw-server/src/lib/auth.ts @@ -0,0 +1,7 @@ +export function set_session_token(token: string) { + window.localStorage.setItem('session', token); +} + +export function get_session_token(): string | null { + return window.localStorage.getItem('session'); +}
\ No newline at end of file diff --git a/web/pw-server/src/routes/login.svelte b/web/pw-server/src/routes/login.svelte new file mode 100644 index 0000000..197238e --- /dev/null +++ b/web/pw-server/src/routes/login.svelte @@ -0,0 +1,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> diff --git a/web/pw-server/src/routes/register.svelte b/web/pw-server/src/routes/register.svelte new file mode 100644 index 0000000..a21cf65 --- /dev/null +++ b/web/pw-server/src/routes/register.svelte @@ -0,0 +1,38 @@ +<script lang="ts"> + let username: string | undefined; + let password: string | undefined; + + const onSubmit = () => { + if (username === undefined || username.trim() === '') { + return; + } + + if (password === undefined || password.trim() === '') { + return; + } + + fetch('/api/register', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + username, + password + }) + }) + .then((resp) => resp.json()) + .then((data) => { + console.log(data); + }); + }; +</script> + +<h1>Register</h1> +<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">Register</button> +</form> |