diff --git a/mod/user/cookie.js b/mod/user/cookie.js index 97713584d..afc0d27cd 100644 --- a/mod/user/cookie.js +++ b/mod/user/cookie.js @@ -18,7 +18,10 @@ module.exports = async (req, res) => { if (!cookie) { // Get login view template. - const no_cookie_found = await languageTemplates('no_cookie_found', req.params.language) + const no_cookie_found = await languageTemplates({ + template: 'no_cookie_found', + language: req.params.language + }) return login(req, res, no_cookie_found) } diff --git a/mod/user/login.js b/mod/user/login.js index f4afcc9a1..50d626a68 100644 --- a/mod/user/login.js +++ b/mod/user/login.js @@ -59,7 +59,10 @@ module.exports = async (req, res, _message) => { } // Get message from templates. - const message = await languageTemplates(req.params.msg || _message, req.params.language) + const message = await languageTemplates({ + template: req.params.msg || _message, + language: req.params.language + }) if (!message && req.params.user) { @@ -95,9 +98,15 @@ async function post(req, res) { && /^[A-Za-z0-9.,_-\s]*$/.test(req.headers['x-forwarded-for']) ? req.headers['x-forwarded-for'] : 'invalid' || 'unknown'; - if(!req.body.email) return new Error(await languageTemplates('missing_email', req.params.language)) + if (!req.body.email) return new Error(await languageTemplates({ + template: 'missing_email', + language: req.params.language + })) - if(!req.body.password) return new Error(await languageTemplates('missing_password', req.params.language)) + if (!req.body.password) return new Error(await languageTemplates({ + template: 'missing_password', + language: req.params.language + })) const date = new Date() @@ -113,15 +122,24 @@ async function post(req, res) { RETURNING email, roles, language, blocked, approved, approved_by, verified, admin, password ${process.env.APPROVAL_EXPIRY ? ', expires_on;' : ';'}`, [req.body.email]) - if (rows instanceof Error) return new Error(await languageTemplates('failed_query', req.params.language)) + if (rows instanceof Error) return new Error(await languageTemplates({ + template: 'failed_query', + language: req.params.language + })) // Get user record from first row. const user = rows[0] - if (!user) return new Error(await languageTemplates('auth_failed', req.params.language)) + if (!user) return new Error(await languageTemplates({ + template: 'auth_failed', + language: req.params.language + })) // Blocked user cannot login. - if (user.blocked) return new Error(await languageTemplates('user_blocked', user.language || req.params.language)) + if (user.blocked) return new Error(await languageTemplates({ + template: 'user_blocked', + language: user.language || req.params.language + })) // Non admin accounts may expire. if (!user.admin && process.env.APPROVAL_EXPIRY) { @@ -139,10 +157,16 @@ async function post(req, res) { WHERE lower(email) = lower($1);`, [req.body.email]) - if (rows instanceof Error) return new Error(await languageTemplates('failed_query', req.params.language)) + if (rows instanceof Error) return new Error(await languageTemplates({ + template: 'failed_query', + language: req.params.language + })) } - return new Error(await languageTemplates('user_expired', user.language)) + return new Error(await languageTemplates({ + template: 'user_expired', + language: user.language + })) } @@ -160,7 +184,10 @@ async function post(req, res) { remote_address }) - return new Error(await languageTemplates('user_not_verified', user.language)) + return new Error(await languageTemplates({ + template: 'user_not_verified', + language: user.language + })) } // Check password from post body against encrypted password from ACL. @@ -181,7 +208,10 @@ async function post(req, res) { WHERE lower(email) = lower($1)`, [req.body.email]) - if (rows instanceof Error) return new Error(await languageTemplates('failed_query', req.params.language)) + if (rows instanceof Error) return new Error(await languageTemplates({ + template: 'failed_query', + language: req.params.language + })) } @@ -201,7 +231,10 @@ async function post(req, res) { WHERE lower(email) = lower($1) RETURNING failedattempts;`, [req.body.email]) - if (rows instanceof Error) return new Error(await languageTemplates('failed_query', req.params.language)) + if (rows instanceof Error) return new Error(await languageTemplates({ + template: 'failed_query', + language: req.params.language + })) // Check whether failed login attempts exceeds limit. if (rows[0].failedattempts >= parseInt(process.env.FAILED_ATTEMPTS || 3)) { @@ -217,7 +250,10 @@ async function post(req, res) { verificationtoken = '${verificationtoken}' WHERE lower(email) = lower($1);`, [req.body.email]) - if (rows instanceof Error) return new Error(await languageTemplates('failed_query', req.params.language)) + if (rows instanceof Error) return new Error(await languageTemplates({ + template: 'failed_query', + language: req.params.language + })) await mailer({ template: 'locked_account', @@ -230,7 +266,10 @@ async function post(req, res) { remote_address }) - return new Error(await languageTemplates('user_locked', user.language)) + return new Error(await languageTemplates({ + template: 'user_locked', + language: user.language + })) } // Login has failed but account is not locked (yet). @@ -242,5 +281,8 @@ async function post(req, res) { remote_address }) - return new Error(await languageTemplates('auth_failed', req.params.language)) + return new Error(await languageTemplates({ + template: 'auth_failed', + language: req.params.language + })) } \ No newline at end of file diff --git a/mod/user/register.js b/mod/user/register.js index 5e6e86595..a573d44e9 100644 --- a/mod/user/register.js +++ b/mod/user/register.js @@ -70,7 +70,10 @@ async function post(req, res) { WHERE lower(email) = lower($1);`, [req.body.email]) - const failed_query = await languageTemplates('failed_query', req.params.language) + const failed_query = await languageTemplates({ + template: 'failed_query', + language: req.params.language + }) if (rows instanceof Error) return res.status(500).send(failed_query) @@ -99,7 +102,10 @@ async function post(req, res) { if (user) { // Blocked user may not reset their password. - if (user.blocked) return res.status(500).send(await languageTemplates('user_blocked', user.language || req.params.language)) + if (user.blocked) return res.status(500).send(await languageTemplates({ + template: 'user_blocked', + language: user.language || req.params.language + })) // Set new password and verification token. // New passwords will only apply after account verification. @@ -112,7 +118,10 @@ async function post(req, res) { WHERE lower(email) = lower($1);`, [req.body.email]) - if (rows instanceof Error) return res.status(500).send(await languageTemplates('failed_query', req.params.language)) + if (rows instanceof Error) return res.status(500).send(await languageTemplates({ + template: 'failed_query', + language: req.params.language + })) // Sent mail with verification token to the account email address. await mailer({ @@ -124,7 +133,10 @@ async function post(req, res) { remote_address }) - const password_reset_verification = await languageTemplates('password_reset_verification', user.language) + const password_reset_verification = await languageTemplates({ + template: 'password_reset_verification', + language: user.language + }) return res.send(password_reset_verification) } @@ -149,7 +161,10 @@ async function post(req, res) { '${verificationtoken}' AS verificationtoken, array['${date}@${req.ips && req.ips.pop() || req.ip}'] AS access_log;`) - if (rows instanceof Error) return res.status(500).send(await languageTemplates('failed_query', req.params.language)) + if (rows instanceof Error) return res.status(500).send(await languageTemplates({ + template: 'failed_query', + language: req.params.language + })) await mailer({ template: 'verify_account', @@ -161,5 +176,8 @@ async function post(req, res) { }) // Return msg. No redirect for password reset. - res.send(await languageTemplates('new_account_registered', language)) + res.send(await languageTemplates({ + template: 'new_account_registered', + language + })) } \ No newline at end of file diff --git a/mod/user/update.js b/mod/user/update.js index fe8f1d183..a99235067 100644 --- a/mod/user/update.js +++ b/mod/user/update.js @@ -26,7 +26,10 @@ module.exports = async (req, res) => { if (rows instanceof Error) { // Get error message from templates. - const error_message = await languageTemplates('failed_query', req.params.language) + const error_message = await languageTemplates({ + template: 'failed_query', + language: req.params.language + }) return res.status(500).send(error_message) } @@ -47,7 +50,10 @@ module.exports = async (req, res) => { }) } - const update_ok = await languageTemplates('update_ok', req.params.user.language) + const update_ok = await languageTemplates({ + template: 'update_ok', + language: req.params.user.language + }) return res.send(update_ok) } \ No newline at end of file diff --git a/mod/user/verify.js b/mod/user/verify.js index 6db19d46c..f2001b266 100644 --- a/mod/user/verify.js +++ b/mod/user/verify.js @@ -20,7 +20,10 @@ module.exports = async (req, res) => { if (rows instanceof Error) { // Get error message from templates. - const error_message = await languageTemplates('failed_query', req.params.language) + const error_message = await languageTemplates({ + template: 'failed_query', + language: req.params.language + }) return res.status(500).send(error_message) } @@ -48,7 +51,10 @@ module.exports = async (req, res) => { if (rows instanceof Error) { // Get error message from templates. - const error_message = await languageTemplates('failed_query', req.params.language) + const error_message = await languageTemplates({ + template: 'failed_query', + language: req.params.language + }) return res.status(500).send(error_message) } @@ -74,7 +80,10 @@ module.exports = async (req, res) => { if (rows instanceof Error) { // Get error message from templates. - const error_message = await languageTemplates('failed_query', req.params.language) + const error_message = await languageTemplates({ + template: 'failed_query', + language: req.params.language + }) return res.status(500).send(error_message) } @@ -99,9 +108,10 @@ module.exports = async (req, res) => { Promise .allSettled(mail_promises) .then(async arr => { - - console.log(arr) - res.send(await languageTemplates('account_await_approval', user.language)) + res.send(await languageTemplates({ + template: 'account_await_approval', + language: user.language + })) }) .catch(error => console.error(error)) diff --git a/mod/utils/languageTemplates.js b/mod/utils/languageTemplates.js index b0fde04dc..17fa70a26 100644 --- a/mod/utils/languageTemplates.js +++ b/mod/utils/languageTemplates.js @@ -2,21 +2,24 @@ const getFrom = require('../provider/getFrom') const workspaceCache = require('../workspace/cache') -module.exports = async (key, lang = 'en') => { +module.exports = async (params) => { - if (key === undefined) return; + if (params.template === undefined) return; + + // Set english as default template language. + params.language ??= 'en' const workspace = await workspaceCache() - if (!Object.hasOwn(workspace.templates, key)) { + if (!Object.hasOwn(workspace.templates, params.template)) { - console.warn(`Template ${key} not found.`) + console.warn(`Template ${params.template} not found.`) return; } - const allLanguages = workspace.templates[key] + const allLanguages = workspace.templates[params.template] - let template = Object.hasOwn(allLanguages, lang)? allLanguages[lang]: allLanguages.en; + let template = Object.hasOwn(allLanguages, params.language)? allLanguages[params.language]: allLanguages.en; if (typeof template === 'string' && Object.hasOwn(getFrom, template.split(':')[0])) { diff --git a/mod/utils/mailer.js b/mod/utils/mailer.js index adabdb567..4528273d7 100644 --- a/mod/utils/mailer.js +++ b/mod/utils/mailer.js @@ -34,7 +34,7 @@ module.exports = async params => { }) } - const template = await languageTemplates(params.template, params.language) + const template = await languageTemplates(params) if (template.text) { diff --git a/mod/view.js b/mod/view.js index c65612c34..9ddb962bd 100644 --- a/mod/view.js +++ b/mod/view.js @@ -59,7 +59,7 @@ module.exports = async (req, res) => { // .filter(entry => entry[0].match(/^SRC_/)) // .forEach(entry => params[entry[0].replace(/^SRC_/, '')]=entry[1]) - const template = await languageTemplates(params.template, params.language) + const template = await languageTemplates(params) const view = template.replace(/[{]{2}([A-Za-z][A-Za-z0-9]*)[}]{2}/g, matched => {