refactor(auth): simplify hasRedirectBareDomain

following change suggestion requested here
https://github.com/TriliumNext/Notes/pull/1668#pullrequestreview-2755816018
This commit is contained in:
Panagiotis Papadopoulos 2025-04-10 19:48:13 +02:00
parent a819166ae5
commit 1979affa8a

View File

@ -40,9 +40,11 @@ function checkAuth(req: Request, res: Response, next: NextFunction) {
return; return;
} else if (!req.session.loggedIn && !noAuthentication) { } 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 // Check if any note has the #shareRoot label
const shareRootNotes = attributes.getNotesWithLabel("shareRoot"); const shareRootNotes = attributes.getNotesWithLabel("shareRoot");
if (shareRootNotes.length === 0) { if (shareRootNotes.length === 0) {
@ -50,23 +52,13 @@ function checkAuth(req: Request, res: Response, next: NextFunction) {
return; return;
} }
} }
res.redirect(redirectToShare ? "share" : "login"); res.redirect(hasRedirectBareDomain ? "share" : "login");
} else { } else {
next(); 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 // for electron things which need network stuff
// currently, we're doing that for file upload because handling form data seems to be difficult // currently, we're doing that for file upload because handling form data seems to be difficult