Skip to content

Commit

Permalink
location workspaces; login routes;
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Oct 27, 2023
1 parent 6f7ab5a commit b9aadf3
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 53 deletions.
12 changes: 5 additions & 7 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion mod/layer/_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br><br>
Expand Down
2 changes: 1 addition & 1 deletion mod/layer/mvt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mod/location/_location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br><br>
Expand Down
2 changes: 1 addition & 1 deletion mod/location/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

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

Expand Down
8 changes: 6 additions & 2 deletions mod/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 10 additions & 3 deletions mod/user/_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,17 @@ module.exports = (req, res) => {
<a href="https://geolytix.github.io/xyz/docs/develop/api/user/">User API</a>`)
}

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)

}
9 changes: 2 additions & 7 deletions mod/user/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 5 additions & 22 deletions mod/user/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')

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

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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')
}
7 changes: 5 additions & 2 deletions mod/user/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion mod/utils/languageTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 8 additions & 2 deletions mod/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit b9aadf3

Please sign in to comment.