diff options
| author | Kai Gunger <kgunger12@gmail.com> | 2026-11-17 02:19:46 -0500 |
|---|---|---|
| committer | Kai Gunger <kgunger12@gmail.com> | 2026-11-17 02:19:46 -0500 |
| commit | 0c9e372e9076749a7603cd0dd822d60cc3de9531 (patch) | |
| tree | 953357333ad85b81ff858772bef4667d38a4070e /scripts | |
| parent | edca74d8558e7fd212e5cd19abaeafada5a0b73e (diff) | |
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/common/common.js | 2 | ||||
| -rw-r--r-- | scripts/common/current-user.js | 34 | ||||
| -rw-r--r-- | scripts/login.js | 31 | ||||
| -rw-r--r-- | scripts/signup.js | 41 | ||||
| -rw-r--r-- | scripts/user/profile.js | 0 | ||||
| -rw-r--r-- | scripts/user/user.js | 0 |
6 files changed, 108 insertions, 0 deletions
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 --- /dev/null +++ b/scripts/user/profile.js diff --git a/scripts/user/user.js b/scripts/user/user.js new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/scripts/user/user.js |