mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Prettied some stuff, and moved submodules around again
This commit is contained in:
parent
d8ef7cdcb0
commit
3f081d8dff
6 changed files with 106 additions and 182 deletions
|
@ -2,7 +2,7 @@ const logger = require('../utils/logger.js')
|
|||
const utils = require('../utils/utils.js')
|
||||
const dbtools = require('../utils/dbtools.js')
|
||||
|
||||
module.exports = function (options) {
|
||||
module.exports = function(options) {
|
||||
const { userDB, jsonResponse, exceptions } = options
|
||||
|
||||
const renderLogin = (req, res) => {
|
||||
|
@ -10,16 +10,16 @@ module.exports = function (options) {
|
|||
if (jsonResponse) {
|
||||
res.json({
|
||||
result: 'nouser',
|
||||
msg: 'You are not logged in'
|
||||
msg: 'You are not logged in',
|
||||
})
|
||||
} else {
|
||||
res.render('login', {
|
||||
devel: process.env.NS_DEVEL
|
||||
devel: process.env.NS_DEVEL,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return function (req, res, next) {
|
||||
return function(req, res, next) {
|
||||
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
|
||||
const sessionID = req.cookies.sessionID
|
||||
const isException = exceptions.some((exc) => {
|
||||
|
@ -29,17 +29,20 @@ module.exports = function (options) {
|
|||
if (process.env.NS_NOUSER) {
|
||||
req.session = {
|
||||
user: {
|
||||
id: 21323
|
||||
id: 21323,
|
||||
},
|
||||
sessionID: sessionID || 111111111111111111,
|
||||
isException: false
|
||||
isException: false,
|
||||
}
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// FIXME Allowing all urls with _next in it, but not in params
|
||||
if (req.url.split('?')[0].includes('_next') || req.url.split('?')[0].includes('well-known/acme-challenge')) {
|
||||
if (
|
||||
req.url.split('?')[0].includes('_next') ||
|
||||
req.url.split('?')[0].includes('well-known/acme-challenge')
|
||||
) {
|
||||
req.session = { isException: true }
|
||||
next()
|
||||
return
|
||||
|
@ -74,34 +77,44 @@ module.exports = function (options) {
|
|||
req.session = {
|
||||
user: user,
|
||||
sessionID: sessionID,
|
||||
isException: isException
|
||||
isException: isException,
|
||||
}
|
||||
|
||||
logger.DebugLog(`ID #${user.id}: ${req.url}`, 'auth', 1)
|
||||
|
||||
UpdateAccess(userDB, user, ip, sessionID)
|
||||
|
||||
dbtools.Update(userDB, 'sessions', {
|
||||
lastAccess: utils.GetDateString()
|
||||
}, {
|
||||
id: sessionID
|
||||
})
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'sessions',
|
||||
{
|
||||
lastAccess: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: sessionID,
|
||||
}
|
||||
)
|
||||
|
||||
dbtools.Update(userDB, 'users', {
|
||||
lastIP: ip,
|
||||
lastAccess: utils.GetDateString()
|
||||
}, {
|
||||
id: user.id
|
||||
})
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'users',
|
||||
{
|
||||
lastIP: ip,
|
||||
lastAccess: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: user.id,
|
||||
}
|
||||
)
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
function UpdateAccess (db, user, ip, sessionID) {
|
||||
function UpdateAccess(db, user, ip, sessionID) {
|
||||
const accesses = dbtools.Select(db, 'accesses', {
|
||||
userId: user.id,
|
||||
ip: ip
|
||||
ip: ip,
|
||||
})
|
||||
|
||||
if (accesses.length === 0) {
|
||||
|
@ -109,16 +122,16 @@ function UpdateAccess (db, user, ip, sessionID) {
|
|||
userID: user.id,
|
||||
ip: ip,
|
||||
sessionID: sessionID,
|
||||
date: utils.GetDateString()
|
||||
date: utils.GetDateString(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function GetUserBySessionID (db, sessionID, req) {
|
||||
function GetUserBySessionID(db, sessionID, req) {
|
||||
logger.DebugLog(`Getting user from db`, 'auth', 2)
|
||||
|
||||
const session = dbtools.Select(db, 'sessions', {
|
||||
id: sessionID
|
||||
id: sessionID,
|
||||
})[0]
|
||||
|
||||
if (!session) {
|
||||
|
@ -126,7 +139,7 @@ function GetUserBySessionID (db, sessionID, req) {
|
|||
}
|
||||
|
||||
const user = dbtools.Select(db, 'users', {
|
||||
id: session.userID
|
||||
id: session.userID,
|
||||
})[0]
|
||||
|
||||
if (user) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue