2021-12-30 16:34:50 +01:00
|
|
|
<script lang="ts">
|
2022-04-09 11:03:37 +02:00
|
|
|
import * as auth from "$lib/auth";
|
|
|
|
import { goto } from "$app/navigation";
|
|
|
|
|
2021-12-31 11:38:06 +01:00
|
|
|
let username: string | undefined;
|
|
|
|
let password: string | undefined;
|
2021-12-30 16:34:50 +01:00
|
|
|
|
2022-04-09 10:18:49 +02:00
|
|
|
let registrationErrors: string[] = [];
|
|
|
|
|
|
|
|
const onSubmit = async () => {
|
2021-12-31 11:38:06 +01:00
|
|
|
if (username === undefined || username.trim() === "") {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-30 16:34:50 +01:00
|
|
|
|
2021-12-31 11:38:06 +01:00
|
|
|
if (password === undefined || password.trim() === "") {
|
2021-12-30 16:34:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-09 10:18:49 +02:00
|
|
|
registrationErrors = [];
|
|
|
|
|
|
|
|
const response = await fetch("/api/register", {
|
2021-12-31 11:38:06 +01:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
}),
|
2022-04-09 10:18:49 +02:00
|
|
|
});
|
|
|
|
|
2022-04-09 11:03:37 +02:00
|
|
|
if (response.ok) {
|
|
|
|
await auth.login({ username, password });
|
|
|
|
goto("/");
|
|
|
|
} else {
|
2022-04-09 10:18:49 +02:00
|
|
|
const resp = await response.json();
|
|
|
|
const error = resp["error"];
|
|
|
|
if (response.status == 422 && error["type"] === "validation_failed") {
|
|
|
|
registrationErrors = error["validation_errors"];
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 11:38:06 +01:00
|
|
|
};
|
2021-12-30 16:34:50 +01:00
|
|
|
</script>
|
|
|
|
|
2022-03-18 19:43:41 +01:00
|
|
|
<div class="page-card">
|
|
|
|
<div class="page-card-content">
|
|
|
|
<h1 class="page-card-header">Create account</h1>
|
2022-04-09 10:18:49 +02:00
|
|
|
{#if registrationErrors.length > 0}
|
|
|
|
<ul class="error-message-list">
|
|
|
|
{#each registrationErrors as errorMessage}
|
|
|
|
<li class="error-message">{errorMessage}</li>
|
|
|
|
{/each}
|
|
|
|
</ul>
|
|
|
|
{/if}
|
|
|
|
|
2022-03-19 21:33:52 +01:00
|
|
|
<form class="account-form" on:submit|preventDefault={onSubmit}>
|
2022-03-18 19:43:41 +01:00
|
|
|
<label for="username">Username</label>
|
|
|
|
<input name="username" bind:value={username} />
|
|
|
|
<label for="password">Password</label>
|
|
|
|
<input type="password" name="password" bind:value={password} />
|
2022-03-19 21:33:52 +01:00
|
|
|
<button type="submit">Submit</button>
|
2022-03-18 19:43:41 +01:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
2022-03-19 21:33:52 +01:00
|
|
|
@import "src/styles/account_forms.scss";
|
2022-04-09 10:18:49 +02:00
|
|
|
|
|
|
|
.error-message-list {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
</style>
|