redirect based on login status

This commit is contained in:
boyska 2022-05-20 19:17:19 +02:00
parent e070a96fb6
commit 61509f6010
2 changed files with 15 additions and 6 deletions

View file

@ -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) {

View file

@ -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)