www.spaceplanner.app

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

commit e322a93e61c31bd0ea31b0c13d0e85767733d055
parent c1f4490de47c44fe860e93a63b2ea0a1515850a2
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Wed,  9 Oct 2024 14:35:55 -0700

Add little patch to allow logging in on editor page

This is in case the user gets logged out with unsaved changes so
they'll be able to login without losing them.

Diffstat:
Mfiles/floorplans/floorplan/main.css | 8++++++++
Mfiles/lib/etc.js | 15++++++++++++---
Mfiles/lib/ui.js | 30+++++++++++++++++++++---------
Mfiles/login/main.js | 3+--
4 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/files/floorplans/floorplan/main.css b/files/floorplans/floorplan/main.css @@ -126,3 +126,11 @@ input.invalid { #selOps > form { display: inline-block; } + +#login { + border: thin solid lightgrey; + background-color: white; + position: absolute; + top: 2em; + left: 2em; +} diff --git a/files/lib/etc.js b/files/lib/etc.js @@ -65,12 +65,21 @@ export function authorize() { function keep_authorized() { return setInterval(function() { - let left = api.authorized_duration() / 60 - if (left < 10) { + let left = Math.floor(api.authorized_duration() / 60) + if (left < 0) { + if (!document.getElementById("login")) { + let params = { + user: localStorage.getItem("username"), + forceUser: true + } + let login = document.body.appendChild(ui.login(params)) + login.id = "login" + } + } else if (left < 30) { console.log("keep_authorized", "refreshing", left, "minutes left") api.refresh_token() } - }, 1000 * 30) + }, 1000) } export function error(message, on) { diff --git a/files/lib/ui.js b/files/lib/ui.js @@ -80,24 +80,34 @@ function toggle_setup_button(a, b) { }, false) } -export function login(callback) { +export function login(options) { + options = options ?? {} + let form = document.createElement("form") form.classList.add("credentials") let h = form.appendChild(document.createElement("h1")) h.append(document.createTextNode("Login")) - let aside = form.appendChild(document.createElement("aside")) - aside.append(document.createTextNode("Don't have an account? ")) - let a = aside.appendChild(document.createElement("a")) - a.href = "/register" - a.append(document.createTextNode("Signup")) - aside.append(document.createTextNode(" now!")) + if (!options.user) { + let aside = form.appendChild(document.createElement("aside")) + aside.append(document.createTextNode("Don't have an account? ")) + let a = aside.appendChild(document.createElement("a")) + a.href = "/register" + a.append(document.createTextNode("Signup")) + aside.append(document.createTextNode(" now!")) + } let label = form.appendChild(document.createElement("label")) label.appendChild(document.createTextNode("Username:")) label.setAttribute("for", "username") - form.appendChild(usernameInput()) + let u = form.appendChild(usernameInput()) + if (options.user) { + u.value = options.user + if (options.forceUser) { + u.setAttribute("disabled", true) + } + } label = form.appendChild(document.createElement("label")) label.appendChild(document.createTextNode("Password:")) @@ -113,7 +123,9 @@ export function login(callback) { api.login(username.value, password.value) .then(function() { form.remove() - callback() + if (options.callback != null) { + options.callback() + } }) .catch(function(err) { etc.error(err + ": Unable to login", form) diff --git a/files/login/main.js b/files/login/main.js @@ -9,8 +9,7 @@ function init() { window.location.href = default_page } - let login = document.body.appendChild(ui.login(function() { window.location.href = default_page })) - + document.body.appendChild(ui.login(function() { window.location.href = default_page })) } window.onload = etc.handle_wrap(init)