From a819166ae5703b62bbe290b1a8f0fed3b010f335 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Thu, 10 Apr 2025 09:56:54 +0200 Subject: [PATCH 1/2] fix(auth): avoid "Error: Option 'redirectBareDomain' doesn't exist" on new installations fixes #1667 --- src/services/auth.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/services/auth.ts b/src/services/auth.ts index 7d55c3d32..38eb19823 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -39,7 +39,9 @@ function checkAuth(req: Request, res: Response, next: NextFunction) { res.redirect('login'); return; } else if (!req.session.loggedIn && !noAuthentication) { - const redirectToShare = options.getOptionBool("redirectBareDomain"); + + const redirectToShare = hasRedirectBareDomain(); + if (redirectToShare) { // Check if any note has the #shareRoot label const shareRootNotes = attributes.getNotesWithLabel("shareRoot"); @@ -54,6 +56,18 @@ function checkAuth(req: Request, res: Response, next: NextFunction) { } } + +// avoid receiving an error, on a new installation, when the DB is not initialized yet +// => getOptionBool uses getOption, which throws an error if the option name is not found +// TriliumNextTODO: potentially refactor getOptionBool instead +function hasRedirectBareDomain() { + try { + return options.getOptionBool("redirectBareDomain"); + } catch(e) { + return false; + } +} + // for electron things which need network stuff // currently, we're doing that for file upload because handling form data seems to be difficult function checkApiAuthOrElectron(req: Request, res: Response, next: NextFunction) { From 1979affa8a6232b704c3a967ed3e14ce1a172d63 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Thu, 10 Apr 2025 19:48:13 +0200 Subject: [PATCH 2/2] refactor(auth): simplify hasRedirectBareDomain following change suggestion requested here https://github.com/TriliumNext/Notes/pull/1668#pullrequestreview-2755816018 --- src/services/auth.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/services/auth.ts b/src/services/auth.ts index 38eb19823..d1d951fc0 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -40,9 +40,11 @@ function checkAuth(req: Request, res: Response, next: NextFunction) { return; } else if (!req.session.loggedIn && !noAuthentication) { - const redirectToShare = hasRedirectBareDomain(); + // cannot use options.getOptionBool currently => it will throw an error on new installations + // TriliumNextTODO: look into potentially creating an getOptionBoolOrNull instead + const hasRedirectBareDomain = options.getOptionOrNull("redirectBareDomain") === "true"; - if (redirectToShare) { + if (hasRedirectBareDomain) { // Check if any note has the #shareRoot label const shareRootNotes = attributes.getNotesWithLabel("shareRoot"); if (shareRootNotes.length === 0) { @@ -50,23 +52,13 @@ function checkAuth(req: Request, res: Response, next: NextFunction) { return; } } - res.redirect(redirectToShare ? "share" : "login"); + res.redirect(hasRedirectBareDomain ? "share" : "login"); } else { next(); } } -// avoid receiving an error, on a new installation, when the DB is not initialized yet -// => getOptionBool uses getOption, which throws an error if the option name is not found -// TriliumNextTODO: potentially refactor getOptionBool instead -function hasRedirectBareDomain() { - try { - return options.getOptionBool("redirectBareDomain"); - } catch(e) { - return false; - } -} // for electron things which need network stuff // currently, we're doing that for file upload because handling form data seems to be difficult