From 18a682fd99b2a7f3a8c9659d64cb295b918a7b78 Mon Sep 17 00:00:00 2001 From: matt wilkie Date: Sun, 24 Nov 2024 21:40:44 -0700 Subject: [PATCH] fixed: mixed custom url and bare domain redirect Previous commit contained mishmash of the abandoned custom share and login redirect urls and simple bare domain redirect. Simply put: was broken and didn't work. Todo: test with a new setup and no initialised db. --- .../type_widgets/options/other/share_settings.js | 2 +- src/public/translations/en/translation.json | 2 +- src/routes/api/options.ts | 3 +-- src/routes/routes.ts | 14 +++----------- src/services/auth.ts | 12 ++++++------ src/services/options_init.ts | 1 + 6 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/public/app/widgets/type_widgets/options/other/share_settings.js b/src/public/app/widgets/type_widgets/options/other/share_settings.js index c0727a852..f6bf20fb3 100644 --- a/src/public/app/widgets/type_widgets/options/other/share_settings.js +++ b/src/public/app/widgets/type_widgets/options/other/share_settings.js @@ -17,7 +17,7 @@ const TPL = `
-

${t('share.show_login_in_share_description')}

+

${t('share.show_login_in_share_description', { hostname: window.location.hostname })}

diff --git a/src/public/translations/en/translation.json b/src/public/translations/en/translation.json index d43623153..e2cf04f2f 100644 --- a/src/public/translations/en/translation.json +++ b/src/public/translations/en/translation.json @@ -1566,6 +1566,6 @@ "redirect_bare_domain": "Redirect bare domain to share", "redirect_bare_domain_description": "When enabled, accessing the root URL (/) will redirect to the share page (/share). Relies on having one more more notes shared, and one having `#shareRoot` attribute defined. (default: false)", "show_login_in_share": "Show login in share theme", - "show_login_in_share_description": "When enabled, shows a login button in the share theme (default: false). Probably best to leave it disabled and visit 'my-domain.net/login' manually to login." + "show_login_in_share_description": "When enabled, shows a login button in the share theme (default: false). Probably best to leave it disabled and visit '{{hostname}}/login' manually to login." } } diff --git a/src/routes/api/options.ts b/src/routes/api/options.ts index fae020a7e..7e5a8dae3 100644 --- a/src/routes/api/options.ts +++ b/src/routes/api/options.ts @@ -68,8 +68,7 @@ export const ALLOWED_OPTIONS = new Set([ 'firstDayOfWeek', 'textNoteEditorType', 'layoutOrientation', - 'shareRedirectUrl', - 'loginRedirectUrl', + 'redirectBareDomain', 'showLoginInShareTheme' ]); diff --git a/src/routes/routes.ts b/src/routes/routes.ts index 1e11f65cb..5adb5e822 100644 --- a/src/routes/routes.ts +++ b/src/routes/routes.ts @@ -15,6 +15,7 @@ import rateLimit from "express-rate-limit"; import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js"; import NotFoundError from "../errors/not_found_error.js"; import ValidationError from "../errors/validation_error.js"; +import optionService from '../services/options.js'; // page routes import setupRoute from "./setup.js"; @@ -54,7 +55,7 @@ import clipperRoute from "./api/clipper.js"; import similarNotesRoute from "./api/similar_notes.js"; import keysRoute from "./api/keys.js"; import backendLogRoute from "./api/backend_log.js"; -import statsRoute from "../stats/stats.js"; +import statsRoute from "./api/stats.js"; import fontsRoute from "./api/fonts.js"; import etapiTokensApiRoutes from "./api/etapi_tokens.js"; import relationMapApiRoute from "./api/relation-map.js"; @@ -102,15 +103,6 @@ const uploadMiddlewareWithErrorHandling = function (req: express.Request, res: e }; function register(app: express.Application) { - // Add bare domain redirect handler - app.use((req, res, next) => { - if (req.path === '/' && optionService.getOption('redirectBareDomain')) { - res.redirect('/share'); - } else { - next(); - } - }); - route(GET, '/', [auth.checkAuth, csrfMiddleware], indexRoute.index); route(GET, '/login', [auth.checkAppInitialized, auth.checkPasswordSet], loginRoute.loginPage); route(GET, '/set-password', [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPasswordPage); @@ -118,7 +110,7 @@ function register(app: express.Application) { const loginRateLimiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 10, // limit each IP to 10 requests per windowMs - skipSuccessfulRequests: true // successful auth to rate-limited ETAPI routes isn't counted. However, successful auth to /login is still counted! + skipSuccessfulRequests: true, // successful auth to rate-limited ETAPI routes isn't counted. However, successful auth to /login is still counted! message: 'Too many login attempts, please try again later.', standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers legacyHeaders: false // Disable the `X-RateLimit-*` headers diff --git a/src/services/auth.ts b/src/services/auth.ts index 0ae4baa9b..c5cdbc9b8 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -14,12 +14,12 @@ import optionService from "./options.js"; const noAuthentication = config.General && config.General.noAuthentication === true; function checkAuth(req: AppRequest, res: Response, next: NextFunction) { - if (!sqlInit.isDbInitialized()) { - res.redirect("setup"); - } - else if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) { - const shareRedirectUrl = optionService.getOption('shareRedirectUrl'); - res.redirect(shareRedirectUrl); + if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) { + if (optionService.getOption('redirectBareDomain') === 'true') { + res.redirect('/share'); + } else { + res.redirect('login'); + } } else { next(); diff --git a/src/services/options_init.ts b/src/services/options_init.ts index 37ebb6adf..da7318d5b 100644 --- a/src/services/options_init.ts +++ b/src/services/options_init.ts @@ -102,6 +102,7 @@ const defaultOptions: DefaultOption[] = [ { name: 'headingStyle', value: 'underline', isSynced: true }, { name: 'autoCollapseNoteTree', value: 'true', isSynced: true }, { name: 'autoReadonlySizeText', value: '10000', isSynced: false }, + { name: 'redirectBareDomain', value: 'false', isSynced: true }, { name: 'autoReadonlySizeCode', value: '30000', isSynced: false }, { name: 'dailyBackupEnabled', value: 'true', isSynced: false }, { name: 'weeklyBackupEnabled', value: 'true', isSynced: false },