From 0c9e372e9076749a7603cd0dd822d60cc3de9531 Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Tue, 17 Nov 2026 02:19:46 -0500 Subject: Add scripting --- index.html | 14 +++++++++----- login.html | 5 +++++ scripts/common/common.js | 2 ++ scripts/common/current-user.js | 34 ++++++++++++++++++++++++++++++++++ scripts/login.js | 31 +++++++++++++++++++++++++++++++ scripts/signup.js | 41 +++++++++++++++++++++++++++++++++++++++++ scripts/user/profile.js | 0 scripts/user/user.js | 0 signup.html | 21 +++++++++++++-------- user/index.html | 37 ++++++++++++++++++++++++++++++++++++- user/profile.html | 39 +++++++++++++++++++++++++++++++++++++-- 11 files changed, 208 insertions(+), 16 deletions(-) create mode 100644 scripts/common/common.js create mode 100644 scripts/common/current-user.js create mode 100644 scripts/login.js create mode 100644 scripts/user/profile.js create mode 100644 scripts/user/user.js diff --git a/index.html b/index.html index fc45261..8062e03 100644 --- a/index.html +++ b/index.html @@ -5,14 +5,14 @@ ComXII - + @@ -37,5 +37,9 @@ + + + + \ No newline at end of file diff --git a/login.html b/login.html index 9f53fab..abb5eca 100644 --- a/login.html +++ b/login.html @@ -44,5 +44,10 @@ + + + + + \ No newline at end of file diff --git a/scripts/common/common.js b/scripts/common/common.js new file mode 100644 index 0000000..256cd21 --- /dev/null +++ b/scripts/common/common.js @@ -0,0 +1,2 @@ +const API_BASE_URL="https://comxii.net/api"; + diff --git a/scripts/common/current-user.js b/scripts/common/current-user.js new file mode 100644 index 0000000..9e29927 --- /dev/null +++ b/scripts/common/current-user.js @@ -0,0 +1,34 @@ +var User = {}; + +/** + * Handles + * @param {ProgressEvent} event + * @this {XMLHttpRequest} + */ +function update_user_data (event) { + let result = JSON.parse(this.responseText); + + if (this.status >= 400) + return; + + User = result; + + // Grab and change user links to point at user orofile, link text to username + /** @type {HTMLLinkElement} */ + let link = document.getElementById("user-link"); + /** @type {HTMLImageElement} */ + let profile_icon = document.getElementById("user-profile"); + + link.href = "/user/" + profile_icon.parentElement.href = "/user/" + link.innerText = "Go To Profile" +} + +// Run at start, grab user information and replace the login link as well as the user object +(() => { + let user_req = new XMLHttpRequest(); + user_req.addEventListener("load", update_user_data); + user_req.open("GET", API_BASE_URL + "/user"); + user_req.send(); +})(); + diff --git a/scripts/login.js b/scripts/login.js new file mode 100644 index 0000000..bc41fd4 --- /dev/null +++ b/scripts/login.js @@ -0,0 +1,31 @@ +(() => { + /** + * Login to the api layer + * @param {ProgressEvent} e + * @this {XMLHttpRequest} + */ + function login (e) { + if (this.status == 200) { + // Go back to home + window.location = "/"; + } else { + // Show invalid username/password text + alert("Invalid username or password!"); + } + } + + /** @type {HTMLInputElement} */ + let username = document.getElementById("username"); + /** @type {HTMLInputElement} */ + let password = document.getElementById("password"); + + document.getElementById("login").addEventListener("click", () => { + let login_req = new XMLHttpRequest(); + login_req.addEventListener("load", login); + login_req.open("POST", API_BASE_URL + "/user/login"); + login_req.send(JSON.stringify({ + username: username.value, + password: password.value, + })); + }) +})(); \ No newline at end of file diff --git a/scripts/signup.js b/scripts/signup.js index e69de29..f07630a 100644 --- a/scripts/signup.js +++ b/scripts/signup.js @@ -0,0 +1,41 @@ +(() => { + /** + * Signup to the api layer + * @param {ProgressEvent} e + * @this {XMLHttpRequest} + */ + function signup (e) { + if (this.status == 200) { + // Go to login + window.location = "./login.html"; + } else { + // Show invalid username/password text + alert(this.responseText); + } + } + + /** @type {HTMLInputElement} */ + let username = document.getElementById("username"); + /** @type {HTMLInputElement} */ + let email = document.getElementById("email"); + /** @type {HTMLInputElement} */ + let password = document.getElementById("password"); + /** @type {HTMLInputElement} */ + let password_confirm = document.getElementById("password_confirm"); + + document.getElementById("signup").addEventListener("click", () => { + if (password.value != password_confirm.value) { + alert("Passwords don't match!"); + return; + } + + let signup_req = new XMLHttpRequest(); + signup_req.addEventListener("load", signup); + signup_req.open("POST", API_BASE_URL + "/user"); + signup_req.send(JSON.stringify({ + username: username.value, + password: password.value, + email: email.value, + })); + }) +})(); \ No newline at end of file diff --git a/scripts/user/profile.js b/scripts/user/profile.js new file mode 100644 index 0000000..e69de29 diff --git a/scripts/user/user.js b/scripts/user/user.js new file mode 100644 index 0000000..e69de29 diff --git a/signup.html b/signup.html index a6cfa89..ba5a590 100644 --- a/signup.html +++ b/signup.html @@ -5,15 +5,15 @@ Sign Up - - + + @@ -39,12 +39,17 @@
- Have an account? Login + Have an account? Login + + + + + \ No newline at end of file diff --git a/user/index.html b/user/index.html index e8ef4a9..886cb9a 100644 --- a/user/index.html +++ b/user/index.html @@ -4,8 +4,43 @@ My Profile + + - + + + + + + + + + + + + \ No newline at end of file diff --git a/user/profile.html b/user/profile.html index 015db48..6b89f0e 100644 --- a/user/profile.html +++ b/user/profile.html @@ -3,9 +3,44 @@ - User Profile + User Profile + + - + + + + + + + + + + + + \ No newline at end of file -- cgit v1.3.1