Files
Trilium/apps/server/src/routes/session_parser.ts

46 lines
1.5 KiB
TypeScript
Raw Normal View History

import sql from "../services/sql.js";
import session, { Store } from "express-session";
import sessionSecret from "../services/session_secret.js";
import config from "../services/config.js";
class SQLiteSessionStore extends Store {
get(sid: string, callback: (err: any, session?: session.SessionData | null) => void): void {
return callback(null);
}
set(id: string, session: session.SessionData, callback?: (err?: any) => void): void {
const expires = Date.now() + 3600000; // Session expiration time (1 hour from now)
const data = JSON.stringify(session);
sql.upsert("sessions", "id", {
id,
expires,
data
});
callback?.();
}
destroy(sid: string, callback?: (err?: any) => void): void {
console.log("Destroy ", sid);
sql.execute(/*sql*/`DELETE FROM sessions WHERE id = ?`, sid);
callback?.();
}
}
2023-05-07 15:23:46 +02:00
const sessionParser = session({
secret: sessionSecret,
resave: false, // true forces the session to be saved back to the session store, even if the session was never modified during the request.
saveUninitialized: false, // true forces a session that is "uninitialized" to be saved to the store. A session is uninitialized when it is new but not modified.
cookie: {
path: "/",
2023-05-07 15:23:46 +02:00
httpOnly: true,
2025-02-13 09:07:25 +01:00
maxAge: config.Session.cookieMaxAge * 1000 // needs value in milliseconds
2023-05-07 15:23:46 +02:00
},
2025-01-09 18:07:02 +02:00
name: "trilium.sid",
store: new SQLiteSessionStore()
2023-05-07 15:23:46 +02:00
});
2025-03-26 00:50:37 +01:00
export default sessionParser;