Add error messages to login screen

This commit is contained in:
Ethan O'Brien 2024-07-11 07:57:18 -05:00
parent 48b6948a76
commit 729c5d5568

View file

@ -5,23 +5,34 @@ import Request from '../Request.jsx'
function Login() { function Login() {
const error = useState(new URL(window.location).searchParams.get("message") || ""); const error = useState(new URL(window.location).searchParams.get("message") || "");
const uid = useState((window.localStorage && window.localStorage.getItem("ew_uid")) || ""); const uid = useState((window.localStorage && window.localStorage.getItem("ew_uid")) || "");
let password; const password = useState("");
function showError(message) {
error[1](message);
}
const handleSubmit = async (event) => { const handleSubmit = async (event) => {
event.preventDefault(); event.preventDefault();
if (!uid[0] || !password || isNaN(uid[0])) return; if (!uid[0] || !password[0]) {
showError("Missing userid/password");
return;
}
if (isNaN(uid[0])) {
showError("UserID should be a number. (The \"Friend ID\" in your profile)");
return;
}
if (window.localStorage) window.localStorage.setItem("ew_uid", uid[0]); if (window.localStorage) window.localStorage.setItem("ew_uid", uid[0]);
let resp = await Request( let resp = await Request(
"/api/webui/login", "/api/webui/login",
{ {
uid: parseInt(uid[0]), uid: parseInt(uid[0]),
password: password password: password[0]
} }
); );
if (resp.result == "OK") { if (resp.result == "OK") {
window.location.href = "/home/"; window.location.href = "/home/";
} else { } else {
error[1](resp.message); showError(resp.message);
} }
}; };
@ -42,7 +53,7 @@ function Login() {
<label htmlFor="id">SIF2 ID:</label> <label htmlFor="id">SIF2 ID:</label>
<input type="text" id="id" name="id" onChange={(event) => {uid[1](event.target.value)}} value={uid[0]} /> <input type="text" id="id" name="id" onChange={(event) => {uid[1](event.target.value)}} value={uid[0]} />
<label htmlFor="password">Transfer passcode:</label> <label htmlFor="password">Transfer passcode:</label>
<input type="password" id="password" name="password" onChange={(event) => {password = event.target.value}} /> <input type="password" id="password" name="password" onChange={(event) => {password[1](event.target.value)}} />
<input type="submit" value="Submit" onClick={handleSubmit}/> <input type="submit" value="Submit" onClick={handleSubmit}/>
<div id="sub_div"> <div id="sub_div">
<button onClick={import_user}>Import User</button><br/><br/> <button onClick={import_user}>Import User</button><br/><br/>