trilium/src/public/javascripts/services/protected_session_holder.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import optionsInitService from './options_init.js';
let lastProtectedSessionOperationDate = null;
let protectedSessionTimeout = null;
let protectedSessionId = null;
optionsInitService.optionsReady.then(options => protectedSessionTimeout = options.protectedSessionTimeout);
setInterval(() => {
2019-02-10 23:36:25 +08:00
if (lastProtectedSessionOperationDate !== null && Date.now() - lastProtectedSessionOperationDate.getTime() > protectedSessionTimeout * 1000) {
resetProtectedSession();
}
}, 5000);
function setProtectedSessionTimeout(encSessTimeout) {
protectedSessionTimeout = encSessTimeout;
}
function getProtectedSessionId() {
return protectedSessionId;
}
function setProtectedSessionId(id) {
protectedSessionId = id;
}
function resetProtectedSession() {
protectedSessionId = null;
// most secure solution - guarantees nothing remained in memory
// since this expires because user doesn't use the app, it shouldn't be disruptive
utils.reloadApp();
}
function isProtectedSessionAvailable() {
return protectedSessionId !== null;
}
function touchProtectedSession() {
if (isProtectedSessionAvailable()) {
lastProtectedSessionOperationDate = new Date();
}
}
export default {
getProtectedSessionId,
setProtectedSessionId,
resetProtectedSession,
isProtectedSessionAvailable,
setProtectedSessionTimeout,
touchProtectedSession
};