diff --git a/api/api.js b/api/api.js index 673069a35..68f95144a 100644 --- a/api/api.js +++ b/api/api.js @@ -105,7 +105,7 @@ module.exports = async (req, res) => { } // Language param will default to english [en] is not explicitly set. - req.params.language = req.params.language || 'en' + req.params.language ??= 'en' // Assign from _template if provided as path param. req.params.template ??= req.params._template @@ -147,8 +147,10 @@ module.exports = async (req, res) => { // Remove cookie. res.setHeader('Set-Cookie', `${process.env.TITLE}=null;HttpOnly;Max-Age=0;Path=${process.env.DIR || '/'};SameSite=Strict${!req.headers.host.includes('localhost') && ';Secure' || ''}`) + req.params.msg = user.msg + // Return login view with error message. - return login(req, res, user.msg) + return login(req, res) } // Set user as request parameter. @@ -163,11 +165,7 @@ module.exports = async (req, res) => { if (req.url.match(/(?<=\/api\/user)/)) { // A msg will be returned if the user does not met the required priviliges. - const msg = routes.user(req, res) - - // Return the login view with the msg. - msg && login(req, res, msg) - return + return routes.user(req, res) } // The login view will be returned for all PRIVATE requests without a valid user. diff --git a/mod/layer/_layer.js b/mod/layer/_layer.js index 788d7cb00..ef44d5242 100644 --- a/mod/layer/_layer.js +++ b/mod/layer/_layer.js @@ -6,7 +6,7 @@ const workspaceCache = require('../workspace/cache') module.exports = async (req, res) => { - const workspace = workspaceCache() + const workspace = await workspaceCache() if (!req.params.layer) { return res.send(`Failed to evaluate 'layer' param.

diff --git a/mod/layer/mvt.js b/mod/layer/mvt.js index a47c3bab8..5fb265d0f 100644 --- a/mod/layer/mvt.js +++ b/mod/layer/mvt.js @@ -12,7 +12,7 @@ const workspaceCache = require('../workspace/cache') module.exports = async (req, res) => { - const workspace = workspaceCache() + const workspace = await workspaceCache() // Check the layer.roles{} against the user.roles[] const layer = Roles.check(req.params.layer, req.params.user?.roles) diff --git a/mod/location/_location.js b/mod/location/_location.js index 0094e1441..7bb1f2036 100644 --- a/mod/location/_location.js +++ b/mod/location/_location.js @@ -9,7 +9,7 @@ const workspaceCache = require('../workspace/cache') module.exports = async (req, res) => { - const workspace = workspaceCache() + const workspace = await workspaceCache() if (!Object.hasOwn(methods, req.params.method)) { return res.send(`Failed to evaluate 'method' param.

diff --git a/mod/location/delete.js b/mod/location/delete.js index 987280854..7bca52cb3 100644 --- a/mod/location/delete.js +++ b/mod/location/delete.js @@ -4,7 +4,7 @@ const workspaceCache = require('../workspace/cache') module.exports = async (req, res) => { - const workspace = workspaceCache() + const workspace = await workspaceCache() const layer = req.params.layer diff --git a/mod/location/get.js b/mod/location/get.js index bfbb58e3b..fce4aa17c 100644 --- a/mod/location/get.js +++ b/mod/location/get.js @@ -8,7 +8,7 @@ const workspaceCache = require('../workspace/cache') module.exports = async (req, res) => { - const workspace = workspaceCache() + const workspace = await workspaceCache() // Check the layer.roles{} against the user.roles[] const layer = Roles.check(req.params.layer, req.params.user?.roles) diff --git a/mod/location/new.js b/mod/location/new.js index 56976eec9..3064e29e6 100644 --- a/mod/location/new.js +++ b/mod/location/new.js @@ -4,7 +4,7 @@ const workspaceCache = require('../workspace/cache') module.exports = async (req, res) => { - const workspace = workspaceCache() + const workspace = await workspaceCache() const layer = req.params.layer diff --git a/mod/location/update.js b/mod/location/update.js index 48dd22575..edde91083 100644 --- a/mod/location/update.js +++ b/mod/location/update.js @@ -4,7 +4,7 @@ const workspaceCache = require('../workspace/cache') module.exports = async (req, res) => { - const workspace = workspaceCache() + const workspace = await workspaceCache() const layer = req.params.layer diff --git a/mod/query.js b/mod/query.js index a4ab6f8a3..67484efed 100644 --- a/mod/query.js +++ b/mod/query.js @@ -28,12 +28,16 @@ module.exports = async (req, res) => { if (template.err) return res.status(500).send(template.err.message) if (!req.params.user && (template.login || template.admin)) { - login(req, res, 'login_required') + + req.params.msg = 'login_required' + login(req, res) return } if (req.params.user && (!req.params.user.admin && template.admin)) { - login(req, res, 'admin_required') + + req.params.msg = 'admin_required' + login(req, res) return } diff --git a/mod/user/_user.js b/mod/user/_user.js index 68098915f..a01126f01 100644 --- a/mod/user/_user.js +++ b/mod/user/_user.js @@ -54,10 +54,17 @@ module.exports = (req, res) => { User API`) } - if (!req.params.user && (method.login || method.admin)) return 'login_required' + if (!req.params.user && (method.login || method.admin)) { - if (req.params.user && (!req.params.user.admin && method.admin)) return 'admin_required' + req.params.msg = 'login_required' + return + } + + if (req.params.user && (!req.params.user.admin && method.admin)) { + + req.params.msg = 'admin_required' + return + } method.handler(req, res) - } \ No newline at end of file diff --git a/mod/user/cookie.js b/mod/user/cookie.js index afc0d27cd..388c0001c 100644 --- a/mod/user/cookie.js +++ b/mod/user/cookie.js @@ -16,14 +16,9 @@ module.exports = async (req, res) => { if (!cookie && req.params.renew) return res.status(401).send('Failed to renew cookie') if (!cookie) { - - // Get login view template. - const no_cookie_found = await languageTemplates({ - template: 'no_cookie_found', - language: req.params.language - }) + req.params.msg = 'no_cookie_found' - return login(req, res, no_cookie_found) + return login(req, res) } jwt.verify( diff --git a/mod/user/login.js b/mod/user/login.js index 50d626a68..efb30736e 100644 --- a/mod/user/login.js +++ b/mod/user/login.js @@ -14,7 +14,7 @@ const view = require('../view') const { nanoid } = require('nanoid') -module.exports = async (req, res, _message) => { +module.exports = async (req, res) => { if (!acl) return res.status(500).send('ACL unavailable.') @@ -58,21 +58,13 @@ module.exports = async (req, res, _message) => { } - // Get message from templates. - const message = await languageTemplates({ - template: req.params.msg || _message, - language: req.params.language - }) - - if (!message && req.params.user) { + if (!req.params.msg && req.params.user) { res.setHeader('location', `${process.env.DIR}`) res.status(302).send() return; } - req.params.msg = message || ' ' - loginView(req, res) } @@ -130,10 +122,7 @@ async function post(req, res) { // Get user record from first row. const user = rows[0] - if (!user) return new Error(await languageTemplates({ - template: 'auth_failed', - language: req.params.language - })) + if (!user) return new Error('auth_failed') // Blocked user cannot login. if (user.blocked) return new Error(await languageTemplates({ @@ -184,10 +173,7 @@ async function post(req, res) { remote_address }) - return new Error(await languageTemplates({ - template: 'user_not_verified', - language: user.language - })) + return new Error('user_not_verified') } // Check password from post body against encrypted password from ACL. @@ -281,8 +267,5 @@ async function post(req, res) { remote_address }) - return new Error(await languageTemplates({ - template: 'auth_failed', - language: req.params.language - })) + return new Error('auth_failed') } \ No newline at end of file diff --git a/mod/user/verify.js b/mod/user/verify.js index f2001b266..019da38e1 100644 --- a/mod/user/verify.js +++ b/mod/user/verify.js @@ -32,9 +32,12 @@ module.exports = async (req, res) => { if (!user) { - res.setHeader('location', `${process.env.DIR}?msg=token_not_found`) + const token_not_found = await languageTemplates({ + template: 'token_not_found', + language: req.params.language + }) - return res.status(302).send() + return res.status(302).send(token_not_found) } // Update user account in ACL with the approval token and remove verification token. diff --git a/mod/utils/languageTemplates.js b/mod/utils/languageTemplates.js index 17fa70a26..4fc7751f5 100644 --- a/mod/utils/languageTemplates.js +++ b/mod/utils/languageTemplates.js @@ -14,7 +14,7 @@ module.exports = async (params) => { if (!Object.hasOwn(workspace.templates, params.template)) { console.warn(`Template ${params.template} not found.`) - return; + return params.template; } const allLanguages = workspace.templates[params.template] diff --git a/mod/view.js b/mod/view.js index 9ddb962bd..e89df4e23 100644 --- a/mod/view.js +++ b/mod/view.js @@ -28,6 +28,11 @@ module.exports = async (req, res) => { params.title ??= process.env.TITLE + params.msg = req.params.msg && await languageTemplates({ + template: req.params.msg, + language: req.params.language + }) + if (req.params.user && typeof req.params.user === 'object') { params.language ??= req.params.user.language @@ -42,8 +47,9 @@ module.exports = async (req, res) => { })) if (!locales.length) { - - return login(req, res, 'no_locales') + + req.params.msg = 'no_locales' + return login(req, res) } // Encode stringified user for template.