Introduce login UI error and success handling

This commit is contained in:
X3F200C 2024-11-09 00:30:33 -05:00
parent f257c5de22
commit fcee377fb4
6 changed files with 55 additions and 14 deletions

View File

@ -106,7 +106,8 @@ electron.ipcMain.handle("login", function (event, username, password) {
}, function (response) { }, function (response) {
if (response.statusCode !== 200) { if (response.statusCode !== 200) {
return resolve({ return resolve({
"status": "unauthenticated" "status": "unauthenticated",
"successful": false
}); });
} }
@ -126,6 +127,7 @@ electron.ipcMain.handle("login", function (event, username, password) {
resolve({ resolve({
"status": "authenticated", "status": "authenticated",
"successful": true,
...data ...data
}); });
} catch (error) { } catch (error) {

View File

@ -1 +1,5 @@
const electron = require("electron"); const electron = require("electron");
electron.contextBridge.exposeInMainWorld("login", function (username, password) {
return electron.ipcRenderer.invoke("login", username, password);
});

36
src/scripts/login.js Normal file
View File

@ -0,0 +1,36 @@
let usernameField;
let passwordField;
function showHint(message) {
hintDisplay.innerText = message;
hintDisplay.style.visibility = "visible";
}
function hintSuccess(message) {
rootElement.attributeStyleMap.set("--hint", "lime");
showHint(message);
}
function hintError(message) {
rootElement.attributeStyleMap.set("--hint", "red");
showHint(message);
}
function tryAuth() {
window.login(usernameField.value, passwordField.value).then(function (response) {
if (!response.successful) {
return hintError("Login failed, invalid credentials !");
}
hintSuccess("Login successful, this modal should disappear shortly.");
}).catch(function (error) {
hintError("An error occurred while logging in : " + error.message);
});
}
window.addEventListener("load", function () {
rootElement = document.querySelector(":root");
hintDisplay = document.querySelector("#hint");
usernameField = document.querySelector("#username-field");
passwordField = document.querySelector("#password-field");
});

View File

@ -1,11 +0,0 @@
let usernameField;
let passwordField;
function tryAuth() {
window.authenticate(usernameField.value, passwordField.value);
}
window.addEventListener("load", function () {
usernameField = document.querySelector("#username-field");
passwordField = document.querySelector("#password-field");
});

View File

@ -8,3 +8,11 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
} }
#hint {
color: var(--hint);
}
input[type="text"], input[type="password"] {
border: 2px solid var(--hint);
}

View File

@ -11,10 +11,12 @@
<main> <main>
<div id="login-container"> <div id="login-container">
<form id="login-form"> <form id="login-form">
<h2>Plutonium login</h2>
<p id="hint" style="visibility: hidden;"></p>
<label for="username">Username :</label> <label for="username">Username :</label>
<input type="text" name="username" id="username-field" placeholder="Your Plutonium username" minLength="2" maxLength="64"> <input type="text" name="username" id="username-field" placeholder="Your Plutonium username" minLength="3" maxLength="16">
<label for="password">Password :</label> <label for="password">Password :</label>
<input type="text" name="password" id="password-field" placeholder="Your Plutonium password" minLength="2" maxLength="64"> <input type="password" name="password" id="password-field" placeholder="Your Plutonium password" minLength="6" maxLength="64">
<span>Create an account at <a href="https://forum.plutonium.pw/register" target="_blank">https://forum.plutonium.pw/register</a> if you don't have one.</span> <span>Create an account at <a href="https://forum.plutonium.pw/register" target="_blank">https://forum.plutonium.pw/register</a> if you don't have one.</span>
<input type="button" value="Log in" onclick="tryAuth();"> <input type="button" value="Log in" onclick="tryAuth();">
</form> </form>