fix autologin toasts

This commit is contained in:
HorizonCode 2024-01-18 14:10:47 +01:00
parent 05866946a3
commit d5b2b8012c
6 changed files with 169 additions and 76 deletions

11
main.js
View File

@ -137,9 +137,18 @@ function registerIPCPipes() {
}
});
ipcMain.handle("ezpplauncher:autologin-active", async (e) => {
const username = config.get("username");
const password = config.get("password");
return username != undefined && password != undefined;
});
ipcMain.handle("ezpplauncher:autologin", async (e) => {
const hwid = getHwId();
const username = config.get("username");
if (username == undefined) {
return { code: 200, message: "No autologin" };
}
const password = cryptUtil.decrypt(config.get("password"), hwid);
const guest = config.get("guest");
if (guest) return { code: 200, message: "Login as guest", guest: true };
@ -172,6 +181,8 @@ function registerIPCPipes() {
};
}
return result;
} else {
config.remove("password");
}
return {
code: 500,

View File

@ -22,6 +22,15 @@ window.addEventListener("login-attempt", async (e) => {
);
});
window.addEventListener("autologin-active", async (e) => {
const autologin = await ipcRenderer.invoke(
"ezpplauncher:autologin-active",
);
window.dispatchEvent(
new CustomEvent("autologin-result", { detail: autologin }),
);
});
window.addEventListener("autologin-attempt", async () => {
const loginResult = await ipcRenderer.invoke("ezpplauncher:autologin");
window.dispatchEvent(

View File

@ -9,7 +9,6 @@ import image from "@rollup/plugin-image";
import sveltePreprocess from "svelte-preprocess";
import typescript from "@rollup/plugin-typescript";
import progress from "rollup-plugin-progress";
import findUnused from "rollup-plugin-unused";
const production = !process.env.ROLLUP_WATCH;
@ -48,8 +47,7 @@ export default {
file: "public/build/bundle.js",
},
plugins: [
findUnused(),
progress({ clearLine: true }),
!production && progress({ clearLine: true }),
svelte({
preprocess: sveltePreprocess({ sourceMap: !production }),
compilerOptions: {
@ -61,7 +59,7 @@ export default {
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: "bundle.css" }),
postcss(),
postcss({ sourceMap: "inline" }),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In

View File

@ -38,6 +38,12 @@
window.dispatchEvent(new CustomEvent("logout"));
currentUser.set(undefined);
currentPage.set(Page.Login);
toast.success("Successfully logged out!", {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 2000,
});
};
window.addEventListener("launchStatusUpdate", (e) => {

View File

@ -15,81 +15,117 @@
const processLogin = async () => {
loading = true;
window.addEventListener(
"login-result",
(e) => {
const customEvent = e as CustomEvent;
const resultData = customEvent.detail;
const wasSuccessful = "user" in resultData;
const loginPromise = new Promise<void>((res, rej) => {
window.addEventListener(
"login-result",
(e) => {
const customEvent = e as CustomEvent;
const resultData = customEvent.detail;
const wasSuccessful = "user" in resultData;
if (!wasSuccessful) {
const errorResult = resultData as Error;
toast.error(errorResult.message, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 1500,
});
loading = false;
return;
}
const userResult = resultData.user as User;
currentUser.set(userResult);
currentPage.set(Page.Launch);
toast.success(`Welcome back, ${userResult.name}!`, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 3000,
});
},
{ once: true }
);
window.dispatchEvent(
new CustomEvent("login-attempt", {
detail: { username, password, saveCredentials },
})
);
};
const tryAutoLogin = async () => {
loading = true;
await new Promise((res) => setTimeout(res, 1500));
window.addEventListener(
"login-result",
(e) => {
const customEvent = e as CustomEvent;
const resultData = customEvent.detail;
const isGuest = "guest" in resultData;
const wasSuccessful = "user" in resultData;
if (isGuest) {
if (!wasSuccessful) {
/* const errorResult = resultData as Error;
toast.error(errorResult.message, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 1500,
}); */
rej();
loading = false;
return;
}
const userResult = resultData.user as User;
currentUser.set(userResult);
currentPage.set(Page.Launch);
toast.success(`Logged in as Guest`, {
res();
toast.success(`Welcome back, ${userResult.name}!`, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 3000,
});
return;
}
if (!wasSuccessful) {
loading = false;
return;
}
const userResult = resultData.user as User;
currentUser.set(userResult);
currentPage.set(Page.Launch);
toast.success(`Welcome back, ${userResult.name}!`, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 3000,
});
loading = false;
},
{ once: true }
);
window.dispatchEvent(
new CustomEvent("login-attempt", {
detail: { username, password, saveCredentials },
})
);
});
toast.promise(
loginPromise,
{
loading: "Logging in...",
success: "Successfully logged in!",
error: "Failed to login.",
},
{ once: true }
{
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 3000,
}
);
};
const tryAutoLogin = async () => {
loading = true;
const loginPromise = new Promise<void>((res, rej) => {
window.addEventListener(
"login-result",
(e) => {
const customEvent = e as CustomEvent;
const resultData = customEvent.detail;
const isGuest = "guest" in resultData;
const wasSuccessful = "user" in resultData;
if (isGuest) {
currentPage.set(Page.Launch);
res();
toast.success(`Logged in as Guest`, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 3000,
});
return;
}
if (!wasSuccessful) {
loading = false;
rej();
return;
}
const userResult = resultData.user as User;
currentUser.set(userResult);
currentPage.set(Page.Launch);
res();
toast.success(`Welcome back, ${userResult.name}!`, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 3000,
});
loading = false;
},
{ once: true }
);
window.dispatchEvent(new CustomEvent("autologin-attempt"));
});
toast.promise(
loginPromise,
{
loading: "Logging in...",
success: "Successfully logged in!",
error: "Failed to login.",
},
{
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 3000,
}
);
window.dispatchEvent(new CustomEvent("autologin-attempt"));
};
const proceedAsGuest = () => {
@ -103,10 +139,24 @@
});
};
if (!$startup) {
startup.set(true);
tryAutoLogin();
}
const shouldAutologin = async () => {
const shouldAutologin = await new Promise<boolean>((res) => {
window.addEventListener("autologin-result", (e) => {
const customEvent = e as CustomEvent;
const resultData = customEvent.detail;
res(resultData);
});
window.dispatchEvent(new CustomEvent("autologin-active"));
});
return shouldAutologin;
};
(async () => {
if (!$startup) {
startup.set(true);
if (await shouldAutologin()) tryAutoLogin();
}
})();
</script>
<main

View File

@ -1,11 +1,14 @@
<script lang="ts">
import { Button, ButtonGroup, Input } from "flowbite-svelte";
import { Button, ButtonGroup, Input, Toggle } from "flowbite-svelte";
import { FolderSolid } from "flowbite-svelte-icons";
import { currentPage } from "../storage/localStore";
import { Page } from "../consts/pages";
let folderPath: string = "";
let patching: boolean = true;
let presence: boolean = true;
window.addEventListener("settings-result", (e) => {
const settings: Record<string, string>[] = (e as CustomEvent).detail;
const osuPath = settings.find((setting) => setting.key == "osuPath");
@ -16,11 +19,27 @@
const setFolderPath = () => {
window.dispatchEvent(new CustomEvent("folder-set"));
};
const togglePatching = () => {
patching = !patching;
};
const togglePresence = () => {
presence = !presence;
};
</script>
<main
class="h-[265px] my-auto flex flex-col justify-center items-center p-5 animate-fadeIn opacity-0"
class="h-[265px] flex flex-col justify-start p-3 animate-fadeIn opacity-0"
>
<div class="flex flex-col gap-2 p-3">
<Toggle class="w-fit" bind:checked={presence} on:click={togglePresence}
>Discord Presence</Toggle
>
<Toggle class="w-fit" bind:checked={patching} on:click={togglePatching}
>Patching</Toggle
>
</div>
<div
class="container flex flex-col items-center justify-center gap-5 rounded-lg p-3"
>