Skip to content

Commit

Permalink
create reqHost util
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Mar 6, 2024
1 parent e94905b commit 140d06c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions mod/user/_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
@module /user
*/

const reqHost = require('../utils/reqHost')

const view = require('../view')

const methods = {
Expand Down Expand Up @@ -64,9 +66,7 @@ module.exports = async (req, res) => {
return res.send(`Failed to evaluate 'method' param.`)
}

req.params.host = `${req.headers.origin
|| req.headers.host && 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`
|| req.headers.referer && new URL(req.headers.referer).origin
req.params.host = reqHost(req)

if (!req.params.user && (method.login || method.admin)) {

Expand Down
6 changes: 3 additions & 3 deletions mod/user/fromACL.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const crypto = require('crypto')

const mailer = require('../utils/mailer')

const reqHost = require('../utils/reqHost')

const languageTemplates = require('../utils/languageTemplates')

const acl = require('./acl')
Expand Down Expand Up @@ -60,9 +62,7 @@ module.exports = async (req) => {
request.date = new Date()

// Get the host for the account verification email.
request.host = `${req.headers.origin
|| req.headers.host && 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`
|| req.headers.referer && new URL(req.headers.referer).origin
request.host = reqHost(req)

const user = await getUser(request)

Expand Down
8 changes: 4 additions & 4 deletions mod/user/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const acl = require('./acl')

const mailer = require('../utils/mailer')

const reqHost = require('../utils/reqHost')

const languageTemplates = require('../utils/languageTemplates')

const view = require('../view')
Expand All @@ -18,9 +20,7 @@ module.exports = async (req, res) => {

if (!acl) return res.status(500).send('ACL unavailable.')

req.params.host = `${req.headers.origin
|| req.headers.host && 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`
|| req.headers.referer && new URL(req.headers.referer).origin
req.params.host = reqHost(req)

// Post request to register new user.
if (req.body && req.body.register) return post(req, res)
Expand Down Expand Up @@ -105,7 +105,7 @@ async function post(req, res) {

// Setting the password to NULL will disable access to the account and prevent resetting the password.
// Checking for password reset to allow registering again before verification.
if (!user.password_reset && user?.password === null) {
if (!user?.password_reset && user?.password === null) {

return res.status(401).send('User account has restricted access')
}
Expand Down
21 changes: 21 additions & 0 deletions mod/utils/reqHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const logger = require('./logger')

module.exports = req => {

let host

if (req.headers.host.startsWith('localhost')) {

host = `http://${process.env.ALIAS||req.headers.host}${process.env.DIR}`
} else if (req.headers.host) {

host = `http://${process.env.ALIAS||req.headers.host}${process.env.DIR}`
} else if (req.headers.referer) {

host = new URL(req.headers.referer).origin
}

logger(host, 'reqhost')

return host
}

0 comments on commit 140d06c

Please sign in to comment.