Skip to content

Commit

Permalink
Ensure the own session is only removed once all other sessions remova…
Browse files Browse the repository at this point in the history
…ls finished. This also ensures our own session is NOT deleted on error so the user can still see the generated error message

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Oct 23, 2023
1 parent 80dba39 commit 5e3f2ca
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions scripts/pi-hole/js/settings-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

var apiSessionsTable = null;
var ownSessionID = null;
var deleted = 0;
var TOTPdata = null;

function renderBool(data, type) {
Expand Down Expand Up @@ -206,9 +207,12 @@ function delSessions(ids) {
return parseInt(value, 10);
});

// Check if own session is selected
// Check if own session is selected and remove it when deleteing multiple
// We need this only when multiple sessions are removed to ensure we do not
// accidentally remove our own session and thus log us out *before* we can
// remove the other sessions
let ownSessionDelete = false;
if (ids.includes(ownSessionID)) {
if (ids.includes(ownSessionID) && ids.length > 1) {
ownSessionDelete = true;
// Strip own session ID from array
ids = ids.filter(function (value) {
Expand All @@ -217,24 +221,24 @@ function delSessions(ids) {
}

// Loop through IDs and delete them
deleted = 0;
for (const id of ids) {
if (Object.hasOwnProperty.call(ids, id)) {
delSession(ids[id]);
}
}

// Delete own session last (if selected)
if (ownSessionDelete) {
delSession(ownSessionID);
delSession(id, ids.length, ownSessionDelete);
}
}

function delSession(id) {
function delSession(id, len, ownSessionDelete) {
$.ajax({
url: "/api/auth/session/" + id,
method: "DELETE",
})
.done(function () {
// Do not reload page when deleting multiple sessions
if (++deleted < len) return;

// All other sessions have been deleted, now delete own session
if (ownSessionDelete) delSession(ownSessionID, 1, false);

if (id !== ownSessionID) {
// Reload table to remove session
apiSessionsTable.ajax.reload();
Expand Down

0 comments on commit 5e3f2ca

Please sign in to comment.