redirect + error print + logout

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2023-01-31 22:46:45 +01:00 committed by Gitea
parent 20cf03818d
commit 49c46f9f72
5 changed files with 48 additions and 10 deletions

View file

@ -1,20 +1,26 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { useForm, validators, HintGroup, Hint, required } from 'svelte-use-form';
const form = useForm();
let promise: Promise<void>;
const form = useForm();
$: submit = async (event: Event) => {
// @ts-ignore
const formData = new URLSearchParams(new FormData(event.target as HTMLFormElement));
self
promise = self
.fetch(`/api/${api_endpoint}`, {
method: 'post',
body: formData
})
.then((response) => response.json())
.catch((err) => {
console.error(err)
.then(async (response) => {
if (response.ok) {
goto('/');
} else {
const res = await response.json();
throw new Error(res.message);
}
});
};
@ -40,6 +46,13 @@
<button disabled={!$form.valid}>Login</button>
</form>
{#await promise}
<p>Logging in...</p>
{:then}
<p />
{:catch error}
<p style="color: red">{error}</p>
{/await}
<style>
:global(.touched:invalid) {

View file

@ -2,4 +2,5 @@
<p>There will be something here one day :)</p>
<a href="/login">Login</a>
<a href="/register">Register</a>
<a href="/register">Register</a>
<a href="/logout">Logout</a>

View file

@ -1,5 +1,5 @@
<script lang="ts">
import LoginForm from "$lib/LoginForm.svelte";
import LoginForm from '$lib/LoginForm.svelte';
</script>
<LoginForm api_endpoint="login" description="Login"/>
<LoginForm api_endpoint="login" description="Login" />

View file

@ -0,0 +1,24 @@
<script lang="ts">
import { goto } from '$app/navigation';
let promise = self
.fetch(`/api/logout`, {
method: 'post'
})
.then(async (response) => {
if (response.ok) {
goto('/');
} else {
const res = await response.json();
throw new Error(res.message);
}
});
</script>
{#await promise}
<p />
{:then}
<p />
{:catch error}
<p style="color: red">{error}</p>
{/await}

View file

@ -1,5 +1,5 @@
<script lang="ts">
import LoginForm from "$lib/LoginForm.svelte";
import LoginForm from '$lib/LoginForm.svelte';
</script>
<LoginForm api_endpoint="create" description="Register an Account"/>
<LoginForm api_endpoint="create" description="Register an Account" />