]> git.cworth.org Git - lmno.games/commitdiff
Add JavaScript handling for the recently-added login form
authorCarl Worth <cworth@cworth.org>
Thu, 21 May 2020 17:13:54 +0000 (10:13 -0700)
committerCarl Worth <cworth@cworth.org>
Thu, 21 May 2020 17:13:54 +0000 (10:13 -0700)
The HTML for this login form isn't seen here. It exists inside of
lmno-server.

lmno.js

diff --git a/lmno.js b/lmno.js
index 4af9878d2ef14e20b517a8902367b23f3c8dbbfd..5cd5802856c79e9fdcad6a738de1bd1cb1175070 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -11,6 +11,37 @@ ${message}
   message_area.insertAdjacentHTML('beforeend', message);
 }
 
   message_area.insertAdjacentHTML('beforeend', message);
 }
 
+function lmno_login_loadend(request, username) {
+  if (request.status === 404) {
+    add_message("danger", "User authentication failed. Please try again.");
+    return;
+  }
+
+  /* Now that user is logged in, advance to the desired page (if any). */
+  const url = new URL(window.location);
+  const next_param = url.searchParams.get('next');
+  if (next_param) {
+    window.location.href = next_param;
+    return;
+  }
+
+  /* Otherwise, just report the successful login. */
+  add_message("success", `User ${username} logged in. Have fun.`);
+}
+
+function lmno_login(form) {
+  const username = form.username.value;
+  const password = form.password.value;
+
+  console.log("In lmno_login with username: " + username);
+  var request = new XMLHttpRequest();
+  request.addEventListener("loadend", () => lmno_login_loadend(request, username));
+
+  request.open("POST", "/login");
+  request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
+  request.send(`{"username": "${username}", "password": "${password}"}`);
+}
+
 function lmno_join_loadend(request, game_id) {
   if (request.status === 404) {
     add_message("danger", game_id + " is not a valid game ID. Try again.");
 function lmno_join_loadend(request, game_id) {
   if (request.status === 404) {
     add_message("danger", game_id + " is not a valid game ID. Try again.");