From 0c9e372e9076749a7603cd0dd822d60cc3de9531 Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Tue, 17 Nov 2026 02:19:46 -0500 Subject: Add scripting --- 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 6 files changed, 108 insertions(+) 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 (limited to 'scripts') 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 -- cgit v1.3.1