aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/login.svelte
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2021-12-30 16:34:50 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2021-12-30 16:34:50 +0100
commit4912a0dbe7b1df1e884b53544943319f8f0c0ebb (patch)
treeb091e8778debd4c4292ceb920265e02cb0ba5687 /web/pw-server/src/routes/login.svelte
parent522f4270e83a85564ecc977aa470fcf8560547e2 (diff)
downloadplanetwars.dev-4912a0dbe7b1df1e884b53544943319f8f0c0ebb.tar.xz
planetwars.dev-4912a0dbe7b1df1e884b53544943319f8f0c0ebb.zip
barebones authentication
Diffstat (limited to 'web/pw-server/src/routes/login.svelte')
-rw-r--r--web/pw-server/src/routes/login.svelte46
1 files changed, 46 insertions, 0 deletions
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>