pikvm/pikvm#1204: Expire user session

This commit is contained in:
Maxim Devaev
2025-02-08 23:30:52 +02:00
parent abbd65a9a0
commit a7c3cdc1ea
8 changed files with 225 additions and 57 deletions

View File

@@ -37,6 +37,7 @@
<link rel="stylesheet" href="../share/css/main.css">
<link rel="stylesheet" href="../share/css/window.css">
<link rel="stylesheet" href="../share/css/modal.css">
<link rel="stylesheet" href="../share/css/radio.css">
<link rel="stylesheet" href="../share/css/login/login.css">
<link rel="stylesheet" href="../share/css/user.css">
<script type="module">import {setRootPrefix} from "../share/js/vars.js";
@@ -73,6 +74,24 @@
<hr>
</td>
</tr>
<tr>
<td>Remember me:&nbsp;</td>
<td>
<div class="radio-box">
<input type="radio" id="expire-radio-3600" name="expire-radio" value="3600"/>
<label for="expire-radio-3600">1h</label>
<input type="radio" id="expire-radio-86400" name="expire-radio" value="86400"/>
<label for="expire-radio-86400">24h</label>
<input type="radio" id="expire-radio-0" name="expire-radio" value="0" checked="checked"/>
<label for="expire-radio-0">Forever</label>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
<tr>
<td></td>
<td>

View File

@@ -1,12 +1,22 @@
extends ../base.pug
mixin radio(name, items)
.radio-box
each item in items
-
let id = `${name}-${item["value"]}`
let checked = (item["checked"] || false)
input(type="radio" id=id name=name value=item["value"] checked=checked)
label(for=id) !{item["title"]}
append vars
-
root_prefix = "../"
title = "PiKVM Login"
main_js = "login/main"
css_list.push("window", "modal", "login/login")
css_list.push("window", "modal", "radio", "login/login")
block body
@@ -26,6 +36,17 @@ block body
tr
td(colspan=2)
hr
tr
td Remember me:&nbsp;
td
+radio("expire-radio", [
{"title": "1h", "value": "3600"},
{"title": "24h", "value": "86400"},
{"title": "Forever", "value": "0", "checked": true},
])
tr
td(colspan=2)
hr
tr
td
td #[button.key#login-button(style="width:100%") Login]

View File

@@ -32,6 +32,13 @@ export function main() {
if (checkBrowser(null, null)) {
initWindowManager();
// Radio is a string container
tools.radio.clickValue("expire-radio", tools.storage.get("login.expire", 0));
tools.radio.setOnClick("expire-radio", function() {
let expire = parseInt(tools.radio.getValue("expire-radio"));
tools.storage.setInt("login.expire", expire);
}, false);
tools.el.setOnClick($("login-button"), __login);
$("user-input").onkeyup = $("passwd-input").onkeyup = $("code-input").onkeyup = function(event) {
if (event.code === "Enter") {
@@ -45,39 +52,43 @@ export function main() {
}
function __login() {
let user = $("user-input").value;
if (user.length === 0) {
let e_user = encodeURIComponent($("user-input").value);
if (e_user.length === 0) {
$("user-input").focus();
} else {
let passwd = $("passwd-input").value + $("code-input").value;
let body = `user=${encodeURIComponent(user)}&passwd=${encodeURIComponent(passwd)}`;
tools.httpPost("api/auth/login", null, function(http) {
switch (http.status) {
case 200:
tools.currentOpen("");
break;
case 403:
wm.error("Invalid credentials").then(__tryAgain);
break;
default: {
let error = "";
if (http.status === 400) {
try {
error = JSON.parse(http.responseText)["result"]["error"];
} catch { /* Nah */ }
}
if (error === "ValidatorError") {
wm.error("Invalid characters in credentials").then(__tryAgain);
} else {
wm.error("Login error", http.responseText).then(__tryAgain);
}
} break;
}
}, body, "application/x-www-form-urlencoded");
__setEnabled(false);
return;
}
let e_passwd = encodeURIComponent($("passwd-input").value + $("code-input").value);
let e_expire = encodeURIComponent(tools.radio.getValue("expire-radio"));
let body = `user=${e_user}&passwd=${e_passwd}&expire=${e_expire}`;
tools.httpPost("api/auth/login", null, function(http) {
switch (http.status) {
case 200:
tools.currentOpen("");
break;
case 403:
wm.error("Invalid credentials").then(__tryAgain);
break;
default: {
let error = "";
if (http.status === 400) {
try {
error = JSON.parse(http.responseText)["result"]["error"];
} catch { /* Nah */ }
}
if (error === "ValidatorError") {
wm.error("Invalid characters in credentials").then(__tryAgain);
} else {
wm.error("Login error", http.responseText).then(__tryAgain);
}
} break;
}
}, body, "application/x-www-form-urlencoded");
__setEnabled(false);
}
function __setEnabled(enabled) {