protected session is now global application state to avoid weird issues with multiple tabs/windows/reloads

This commit is contained in:
zadam
2021-05-07 22:23:49 +02:00
parent fddab59265
commit de6108f95d
11 changed files with 49 additions and 82 deletions

View File

@@ -8,11 +8,11 @@ const passwordEncryptionService = require('../../services/password_encryption');
const protectedSessionService = require('../../services/protected_session');
const appInfo = require('../../services/app_info');
const eventService = require('../../services/events');
const cls = require('../../services/cls');
const sqlInit = require('../../services/sql_init');
const sql = require('../../services/sql');
const optionService = require('../../services/options');
const ApiToken = require('../../entities/api_token');
const ws = require("../../services/ws.js");
function loginSync(req) {
if (!sqlInit.schemaExists()) {
@@ -65,16 +65,14 @@ function loginToProtectedSession(req) {
const decryptedDataKey = passwordEncryptionService.getDataKey(password);
const protectedSessionId = protectedSessionService.setDataKey(decryptedDataKey);
// this is set here so that event handlers have access to the protected session
cls.set('protectedSessionId', protectedSessionId);
protectedSessionService.setDataKey(decryptedDataKey);
eventService.emit(eventService.ENTER_PROTECTED_SESSION);
ws.sendMessageToAllClients({ type: 'protectedSessionLogin' });
return {
success: true,
protectedSessionId: protectedSessionId
success: true
};
}
@@ -82,6 +80,8 @@ function logoutFromProtectedSession() {
protectedSessionService.resetDataKey();
eventService.emit(eventService.LEAVE_PROTECTED_SESSION);
ws.sendMessageToAllClients({ type: 'protectedSessionLogout' });
}
function token(req) {

View File

@@ -7,6 +7,7 @@ const config = require('../services/config');
const optionService = require('../services/options');
const log = require('../services/log');
const env = require('../services/env');
const protectedSessionService = require("../services/protected_session.js");
function index(req, res) {
const options = optionService.getOptionsMap();
@@ -30,7 +31,8 @@ function index(req, res) {
appCssNoteIds: getAppCssNoteIds(),
isDev: env.isDev(),
isMainWindow: !req.query.extra,
extraHoistedNoteId: req.query.extraHoistedNoteId
extraHoistedNoteId: req.query.extraHoistedNoteId,
isProtectedSessionAvailable: protectedSessionService.isProtectedSessionAvailable()
});
}

View File

@@ -93,7 +93,6 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
cls.set('sourceId', req.headers['trilium-source-id']);
cls.set('localNowDateTime', req.headers['trilium-local-now-datetime']);
cls.set('hoistedNoteId', req.headers['trilium-hoisted-note-id'] || 'root');
protectedSessionService.setProtectedSessionId(req);
const cb = () => routeHandler(req, res, next);