add useful login statuses

This commit is contained in:
Arthur Lu 2022-12-12 17:12:11 -08:00
parent ad9969c393
commit ccf7efb9b9
3 changed files with 21 additions and 5 deletions

View File

@ -19,6 +19,7 @@
<button id="submit">LOGIN</button> <button id="submit">LOGIN</button>
</div> </div>
</fieldset> </fieldset>
<p id="status"></p>
</form> </form>
</div> </div>
</body> </body>

View File

@ -1,20 +1,35 @@
import {requestTicket, setTicket} from "./utils.js"; import {requestTicket, setTicket, ResponseError, NetworkError} from "./utils.js";
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
function init (){ function init (){
let formSubmitButton = document.querySelector("#submit"); let formSubmitButton = document.querySelector("#submit");
let status = document.querySelector("#status");
formSubmitButton.addEventListener("click", async (e) => { formSubmitButton.addEventListener("click", async (e) => {
status.innerText = "";
e.preventDefault(); e.preventDefault();
let form = document.querySelector("form"); let form = document.querySelector("form");
let formData = new FormData(form); let formData = new FormData(form);
try { try {
status.innerText = "Authenticating...";
let ticket = await requestTicket(formData.get("username"), formData.get("password")); let ticket = await requestTicket(formData.get("username"), formData.get("password"));
await setTicket(ticket.data.ticket); setTicket(ticket.data.ticket);
window.location.href = "index.html"; window.location.href = "index.html";
} }
catch (error) { catch (error) {
console.log(error); if(error instanceof ResponseError) { // response error is usually 401 auth failed
status.innerText = "Authentication failed.";
status.style.color = "#ff0000";
}
else if (error instanceof NetworkError) {
status.innerText = "Encountered a network error.";
status.style.color = "#ff0000";
}
else {
status.innerText = "An error occured.";
status.style.color = "#ff0000";
console.error(error);
}
} }
}); });
} }

View File

@ -1,11 +1,11 @@
class ResponseError extends Error { export class ResponseError extends Error {
constructor(message) { constructor(message) {
super(message); super(message);
this.name = "ResponseError"; this.name = "ResponseError";
} }
} }
class NetworkError extends Error { export class NetworkError extends Error {
constructor(message) { constructor(message) {
super(message); super(message);
this.name = "NetworkError"; this.name = "NetworkError";