diff options
author | stuebinm | 2024-05-03 21:38:55 +0200 |
---|---|---|
committer | stuebinm | 2024-05-03 21:47:43 +0200 |
commit | 0febc9cd99e0d8b80b1385593e25e7670d5c842b (patch) | |
tree | efad9b6d7503e43c86ab88a621a54374744cb5da /site | |
parent | fd31997771fd932a9eb1cc2be7b2b8ed052041f3 (diff) |
onboard/tracker: remember token as query parameter
there's some trade-offs here (cookies may be discarded by the browser,
query parameters lost if people use a bookmark), but either works fine
for not re-issuing tokens if someone reloads the web site.
Diffstat (limited to 'site')
-rw-r--r-- | site/tracker.hamlet | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/site/tracker.hamlet b/site/tracker.hamlet index 5877c5d..2cc05e4 100644 --- a/site/tracker.hamlet +++ b/site/tracker.hamlet @@ -1,7 +1,7 @@ <h1>_{MsgOBU} <section> - <h2>Blub + <h2>Tracker <strong>Token:</strong> <span id="token"> <section> <h2>Status @@ -116,21 +116,31 @@ async function main() { initGeopos(); - token = await (await fetch("/api/tracker/register/", { - method: "POST", - body: JSON.stringify({agent: "tracktrain-website"}), - headers: {"Content-Type": "application/json"} - })).json(); + let urlparams = new URLSearchParams(window.location.search); - if (token.error) { - alert("could not obtain token: \n" + token.msg); - setStatus("_{MsgTokenFailed}"); - } else { - console.log("got token"); + token = urlparams.get("token"); + + if (token === null) { + token = await (await fetch("/api/tracker/register/", { + method: "POST", + body: JSON.stringify({agent: "tracktrain-website"}), + headers: {"Content-Type": "application/json"} + })).json(); + + if (token.error) { + alert("could not obtain token: \n" + token.msg); + setStatus("_{MsgTokenFailed}"); + } else { + console.log("got token"); + window.location.search = `?token=${token}`; + } + } - document.getElementById("token").innerText = token; + console.log(token) - openWebsocket(); + if (token !== null) { + document.getElementById("token").innerText = token; + openWebsocket(); } } |