From 61509f6010efb8e1d4c5f05cc036723805b7872c Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 20 May 2022 19:17:19 +0200 Subject: [PATCH] redirect based on login status --- static/js/change.js | 11 +++++------ static/js/login.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/static/js/change.js b/static/js/change.js index ee1fd8e..b4715c8 100644 --- a/static/js/change.js +++ b/static/js/change.js @@ -1,11 +1,10 @@ async function checkLogin() { - let resp = await fetch('../whoami', { - method: 'POST', - }) - console.log('ok', resp) + let resp = await fetch('../whoami') + if (!resp.ok) { + console.log("We're not logged in, let's go to the login page", resp) + document.location.href = 'login.html' + } data = await resp.json() - console.log('got', data) - // try... catch? } async function onGenerate(evt) { diff --git a/static/js/login.js b/static/js/login.js index a10bde6..0407db4 100644 --- a/static/js/login.js +++ b/static/js/login.js @@ -1,3 +1,12 @@ +async function checkLogin() { + let resp = await fetch('../whoami') + if (resp.ok) { + console.log("Already logged in, let's change password instead") + document.location.href = 'change.html' + } + data = await resp.json() +} + async function onSubmit(evt) { evt.preventDefault() @@ -14,6 +23,7 @@ async function onSubmit(evt) { } function initLogin() { + checkLogin() document.getElementById("login-form").addEventListener('submit', onSubmit, false) } document.addEventListener('DOMContentLoaded', initLogin)