Skip to content

Commit

Permalink
param argument for languageTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Oct 26, 2023
1 parent a4004e8 commit 0625c27
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 37 deletions.
5 changes: 4 additions & 1 deletion mod/user/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
70 changes: 56 additions & 14 deletions mod/user/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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()

Expand All @@ -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) {
Expand All @@ -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
}))

}

Expand All @@ -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.
Expand All @@ -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
}))

}

Expand All @@ -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)) {
Expand All @@ -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',
Expand All @@ -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).
Expand All @@ -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
}))
}
30 changes: 24 additions & 6 deletions mod/user/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand All @@ -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({
Expand All @@ -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)
}
Expand All @@ -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',
Expand All @@ -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
}))
}
10 changes: 8 additions & 2 deletions mod/user/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
22 changes: 16 additions & 6 deletions mod/user/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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))

Expand Down
15 changes: 9 additions & 6 deletions mod/utils/languageTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {

Expand Down
2 changes: 1 addition & 1 deletion mod/utils/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = async params => {
})
}

const template = await languageTemplates(params.template, params.language)
const template = await languageTemplates(params)

if (template.text) {

Expand Down
2 changes: 1 addition & 1 deletion mod/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {

Expand Down

0 comments on commit 0625c27

Please sign in to comment.