add login mechanic

This commit is contained in:
2024-01-11 01:00:43 +01:00
parent 6790fe5ef6
commit 05b9ddd5a1
7 changed files with 138 additions and 67 deletions

View File

@@ -2,7 +2,10 @@
import { Input, Button, Spinner } from "flowbite-svelte";
import { performLogin } from "../util/loginUtil";
import type { User } from "../types/user";
import { currentUser } from "../storage/localStore";
import type { Error } from "../types/error";
import { currentPage, currentUser } from "../storage/localStore";
import toast from "svelte-french-toast";
import { Page } from "../consts/pages";
let loading = false;
let username = "";
@@ -10,11 +13,38 @@
const processLogin = async () => {
loading = true;
const loginResult = await performLogin(username, password);
if (loginResult instanceof Error) {
loading = false;
return;
}
window.addEventListener(
"login-result",
(e) => {
const customEvent = e as CustomEvent;
const resultData = customEvent.detail;
const wasSuccessful = "user" in resultData;
if (!wasSuccessful) {
toast.error(resultData.message, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 1500,
});
loading = false;
return;
}
console.log(resultData);
currentUser.set(resultData.user as User);
currentPage.set(Page.Launch);
toast.success(`Welcome back ${resultData.user.name}!`, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 5000,
});
},
{ once: true }
);
window.dispatchEvent(
new CustomEvent("login-attempt", { detail: { username, password } })
);
};
</script>
@@ -52,7 +82,9 @@
<Button
class="!bg-transparent border-none dark:text-gray-700 hover:!bg-gray-700/15 ring-primary active:ring-2 focus:ring-2"
color="none"
disabled={loading}>Continue without login</Button
disabled={loading}
on:click={() => currentPage.set(Page.Launch)}
>Continue without login</Button
>
</div>
</div>