www.spaceplanner.app

Web client to the spaceplanner API
git clone git://jacobedwards.org/www.spaceplanner.app
Log | Files | Refs

commit e62d08faca95ef8bccf0b89fabebd27e045d73c1
parent 99e5567ec51330edb7ae57aeb17f32996a0a34ac
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Mon,  5 Aug 2024 17:00:36 -0700

Add account creation and deletion functionality

Diffstat:
Mfiles/index.html | 2+-
Mfiles/lib/etc.js | 11+++++++++++
Mfiles/login/index.html | 2++
Mfiles/login/main.js | 2+-
Mfiles/logout/main.js | 2+-
Afiles/register/index.html | 23+++++++++++++++++++++++
Afiles/register/main.js | 38++++++++++++++++++++++++++++++++++++++
Mfiles/settings/main.js | 44+++++++++++++++++++++++++++++++++++++++++++-
Dfiles/success.html | 2--
9 files changed, 120 insertions(+), 6 deletions(-)

diff --git a/files/index.html b/files/index.html @@ -6,5 +6,5 @@ <h1>Spaceplanner</h1> <p>Easy. Fast. Simple.</p> - <p><a href="/login">Login</a> or <a href="/signup.html">signup</a> now!</p> + <p><a href="/login">Login</a> or <a href="/register">signup</a> now!</p> </html> diff --git a/files/lib/etc.js b/files/lib/etc.js @@ -21,3 +21,14 @@ function set_error(message, on) { on.before(err_elem) } } + +function handle_wrap(func, on) { + return function() { + try { + func() + } + catch(err) { + set_error("There was an issue with the page: " + err, on) + } + } +} diff --git a/files/login/index.html b/files/login/index.html @@ -7,6 +7,8 @@ <script src="./main.js" async></script> </head> <html> + <p>Don't have an account? <a href="/register">signup</a></p> + <h1>Login</h1> <form id="login"> diff --git a/files/login/main.js b/files/login/main.js @@ -35,4 +35,4 @@ function init() { }; } -window.onload = init +window.onload = handle_wrap(init) diff --git a/files/logout/main.js b/files/logout/main.js @@ -5,4 +5,4 @@ function init() { window.location.href = default_page } -window.onload = init +window.onload = handle_wrap(init) diff --git a/files/register/index.html b/files/register/index.html @@ -0,0 +1,23 @@ +<!DOCTYPE HTML> +<head> + <title>Spaceplanner - Register</title> + <link rel="stylesheet" type="text/css" href="/css/main.css"> + <script src="/lib/api.js" async></script> + <script src="/lib/etc.js" async></script> + <script src="./main.js" async></script> +</head> +<html> + <p>Already have an account? <a href="/login">login</a> instead.</p> + + <h1>Create New Account</h1> + + <form id="register"> + <label for="username">Username:</label> + <input id="username" autocomplete="username" name="username"/> + + <label for="password">Password:</label> + <input id="password" autocomplete="new-password" type="password" name="password"/> + + <input type="submit" value="Create User"/> + </form> +</html> diff --git a/files/register/main.js b/files/register/main.js @@ -0,0 +1,38 @@ +default_page = "/floorplans" + +function handle_creation(resp) { + window.location.href = "/login" +} + +function register(username, password, err_callback) { + api_fetch("POST", "users", { "username": username, "password": password }) + .then(handle_creation) + .catch(err_callback) + return false; +} + +function init() { + if (api_authorized_duration() > 0) { + // Maybe don't do this? + window.location.href = default_page + } + + let username_input = document.getElementById("username") + let password_input = document.getElementById("password") + if (!username_input || !password_input) { + throw new Error("unable to select username or password") + } + + let form = document.getElementById("register") + if (!form) { + throw new Error("unable to get register form") + } + form.onsubmit = function () { + return register( + username_input.value, password_input.value, + function (error) { return set_error(error, form) } + ); + }; +} + +window.onload = handle_wrap(init) diff --git a/files/settings/main.js b/files/settings/main.js @@ -14,6 +14,48 @@ function main() { }) }) .catch(errfunc) + + profile = document.createElement("h2") + profile.appendChild(document.createTextNode("Profile")) + document.body.append(profile) + del = delete_form() + del.onsubmit = delete_user + document.body.append(del) +} + +function delete_form() { + form = document.createElement("form") + form.id = "delete_user_form" + + label = document.createElement("label") + label.setAttribute("for", "delete_user_confirm") + label.appendChild(document.createTextNode("Confirm ")) + form.append(label) + + check = document.createElement("input") + check.id = "delete_user_confirm" + check.type = "checkbox" + check.setAttribute("required", true) + form.append(check) + + submit = document.createElement("input") + submit.type = "submit" + submit.value = "Delete User" + form.append(submit) + + form.appendChild(document.createTextNode("This action cannot be undone.")) + + return form +} + +function delete_user() { + api_fetch("DELETE", "users/" + localStorage.getItem("username")) + .then(function() { + api_update_token(null) + document.location.href = "/" + }) + .catch(function(err) { set_error("Unable to delete user: " + err, document.getElementById("#delete_form")) }) + return false } function show_settings(current, params) { @@ -91,4 +133,4 @@ function create_input(name, setting, current_value) { return input } -window.onload = init +window.onload = handle_wrap(init) diff --git a/files/success.html b/files/success.html @@ -1,2 +0,0 @@ -<!DOCTYPE HTML> -<html><body><h1>Success, Logged in</h1></body></html>