www.spaceplanner.app

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

commit 3daf877f031afeeb8596ca5fbbc63ad190423ef5
parent 53f6b3bd0a6594f585ff52eca056419de107dcd4
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Fri, 16 Aug 2024 18:44:35 -0700

Periodically refresh authentication token

This was added to etc.authorize(), so nothing else needs to be
called.

Diffstat:
Mfiles/lib/api.js | 8+++-----
Mfiles/lib/etc.js | 11+++++++++++
2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/files/lib/api.js b/files/lib/api.js @@ -51,14 +51,14 @@ function api_fetch(method, endpoint, body) { export { api_fetch as fetch } export function refresh_token() { - api_fetch("GET", "tokens/refresh") + api_fetch("GET", "tokens") .then(function(resp) { update_token(resp.token) }) } export function update_token(t) { - console.log("update_token(" + t + ")") + console.log("update_token", t) if (!t) { localStorage.removeItem("token") localStorage.removeItem("username") @@ -69,9 +69,7 @@ export function update_token(t) { } export function token() { - let t = localStorage.getItem("token") - console.debug("token() > " + t) - return t + return localStorage.getItem("token") } export function token_payload(t) { diff --git a/files/lib/etc.js b/files/lib/etc.js @@ -46,6 +46,17 @@ export function authorize() { // back to the page that was trying to be accessed window.location.href = "/login" } + keep_authorized() +} + +function keep_authorized() { + return setInterval(function() { + let left = api.authorized_duration() / 60 + if (left < 10) { + console.log("keep_authorized", "refreshing", left, "minutes left") + api.refresh_token() + } + }, 1000 * 30) } export function error(message, on) {