Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(server): unable to do first setup
Elian Doran6d376731e398
diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.tsindex 899156b3e2..f3878970b8 100644--- a/apps/server/src/routes/index.ts+++ b/apps/server/src/routes/index.ts@@ -16,8 +16,6 @@ import { generateCsrfToken } from "./csrf_protection.js";type View = "desktop" | "mobile" | "print";export function bootstrap(req: Request, res: Response) {- const options = optionService.getOptionMap();-// csrf-csrf v4 binds CSRF tokens to the session ID via HMAC. With saveUninitialized: false,// a brand-new session is never persisted unless explicitly modified, so its cookie is never// sent to the browser — meaning every request gets a different ephemeral session ID, and@@ -30,12 +28,15 @@ export function bootstrap(req: Request, res: Response) {const isDbInitialized = sql_init.isDbInitialized();const commonItems = getSharedBootstrapItems(assetPath, isDbInitialized);if (!isDbInitialized) {- return {+ res.send({...commonItems,baseApiUrl: "../api/"- };+ });+ return;}+ const options = optionService.getOptionMap();+const csrfToken = generateCsrfToken(req, res, {overwrite: false,validateOnReuse: false // if validation fails, generate a new token instead of throwing an error@@ -51,6 +52,7 @@ export function bootstrap(req: Request, res: Response) {res.send({...commonItems,+ dbInitialized: true,device: view,csrfToken,themeCssUrl: getThemeCssUrl(theme, themeNote),diff --git a/apps/server/src/routes/session_parser.ts b/apps/server/src/routes/session_parser.tsindex a52a9183aa..f5530b37a1 100644--- a/apps/server/src/routes/session_parser.ts+++ b/apps/server/src/routes/session_parser.ts@@ -1,4 +1,5 @@import sql from "../services/sql.js";+import sqlInit from "../services/sql_init.js";import session, { Store } from "express-session";import sessionSecret from "../services/session_secret.js";import config from "../services/config.js";@@ -20,6 +21,10 @@ export const SESSION_COOKIE_EXPIRY = 24 * 60 * 60 * 1000; // 24 hoursexport class SQLiteSessionStore extends Store {get(sid: string, callback: (err: any, session?: session.SessionData | null) => void): void {+ if (!sqlInit.isDbInitialized()) {+ return callback(null, null);+ }+try {const data = sql.getValue<string>(/*sql*/`SELECT data FROM sessions WHERE id = ?`, sid);let session = null;@@ -34,6 +39,10 @@ export class SQLiteSessionStore extends Store {}set(id: string, session: session.SessionData, callback?: (err?: any) => void): void {+ if (!sqlInit.isDbInitialized()) {+ return callback?.();+ }+try {const expires = session.cookie?.expires? new Date(session.cookie.expires).getTime()@@ -53,6 +62,10 @@ export class SQLiteSessionStore extends Store {}destroy(sid: string, callback?: (err?: any) => void): void {+ if (!sqlInit.isDbInitialized()) {+ return callback?.();+ }+try {sql.execute(/*sql*/`DELETE FROM sessions WHERE id = ?`, sid);callback?.();@@ -63,6 +76,10 @@ export class SQLiteSessionStore extends Store {}touch(sid: string, session: session.SessionData, callback?: (err?: any) => void): void {+ if (!sqlInit.isDbInitialized()) {+ return callback?.();+ }+// For now it's only for session cookies ("Remember me" unchecked).if (session.cookie?.expires) {callback?.();diff --git a/apps/server/src/services/auth.ts b/apps/server/src/services/auth.tsindex b10ef80977..a20354f7ff 100644--- a/apps/server/src/services/auth.ts+++ b/apps/server/src/services/auth.ts@@ -16,7 +16,9 @@ refreshAuth();function checkAuth(req: Request, res: Response, next: NextFunction) {if (!sqlInit.isDbInitialized()) {- return res.redirect('setup');+ // DB not initialized — let the request through so the client app+ // can show its setup UI based on the bootstrap response.+ return next();}const currentTotpStatus = totp.isTotpEnabled();@@ -73,6 +75,10 @@ export function refreshAuth() {// for electron things which need network stuff// currently, we're doing that for file upload because handling form data seems to be difficultfunction checkApiAuthOrElectron(req: Request, res: Response, next: NextFunction) {+ if (!sqlInit.isDbInitialized()) {+ return next();+ }+if (!req.session.loggedIn && !isElectron && !noAuthentication) {console.warn(`Missing session with ID '${req.sessionID}'.`);reject(req, res, "Logged in session not found");@@ -82,6 +88,10 @@ function checkApiAuthOrElectron(req: Request, res: Response, next: NextFunction)}function checkApiAuth(req: Request, res: Response, next: NextFunction) {+ if (!sqlInit.isDbInitialized()) {+ return next();+ }+if (!req.session.loggedIn && !noAuthentication) {console.warn(`Missing session with ID '${req.sessionID}'.`);reject(req, res, "Logged in session not found");@@ -90,12 +100,9 @@ function checkApiAuth(req: Request, res: Response, next: NextFunction) {}}-function checkAppInitialized(req: Request, res: Response, next: NextFunction) {- if (!sqlInit.isDbInitialized()) {- res.redirect("setup");- } else {- next();- }+function checkAppInitialized(_req: Request, _res: Response, next: NextFunction) {+ // Let the client app handle the uninitialized state via its setup UI.+ next();}function checkPasswordSet(req: Request, res: Response, next: NextFunction) {diff --git a/packages/trilium-core/src/services/bootstrap_utils.ts b/packages/trilium-core/src/services/bootstrap_utils.tsindex 302bafa2b6..c541407f4a 100644--- a/packages/trilium-core/src/services/bootstrap_utils.ts+++ b/packages/trilium-core/src/services/bootstrap_utils.ts@@ -1,4 +1,4 @@-import { BootstrapCommonItems, BootstrapDefinition } from "@triliumnext/commons";+import { BootstrapDefinition } from "@triliumnext/commons";import { getSql } from "./sql";import protected_session from "./protected_session";import { generateCss, generateIconRegistry, getIconPacks, MIME_TO_EXTENSION_MAPPINGS } from "./icon_packs";@@ -9,7 +9,7 @@ export default function getSharedBootstrapItems(assetPath: string, dbInitializedconst sql = getSql();const currentLocale = getCurrentLocale();- const commonItems: Partial<BootstrapCommonItems> = {+ const commonItems = {assetPath,dbInitialized,...getIconConfig(assetPath)diff --git a/packages/trilium-core/src/services/i18n.ts b/packages/trilium-core/src/services/i18n.tsindex 88fc53c64e..275b1711ff 100644--- a/packages/trilium-core/src/services/i18n.ts+++ b/packages/trilium-core/src/services/i18n.ts@@ -44,6 +44,11 @@ export async function changeLanguage(locale: string) {}export function getCurrentLocale() {+ if (!sql_init.isDbInitialized()) {+ // If DB is not initialized, we cannot get the locale from options, so we return English as a default.+ return LOCALES.find(l => l.id === "en")!;+ }+const localeId = options.getOptionOrNull("locale") ?? "en";const currentLocale = LOCALES.find(l => l.id === localeId);if (!currentLocale) return LOCALES.find(l => l.id === "en")!;