mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Exit cleanup functions, authentication polish, db polish
This commit is contained in:
parent
fb8e12f8d2
commit
c764c4f402
10 changed files with 314 additions and 229 deletions
|
@ -1,13 +1,8 @@
|
|||
const logger = require('../utils/logger.js')
|
||||
const dbtools = require('../utils/dbtools.js')
|
||||
|
||||
const exceptions = [
|
||||
'favicon',
|
||||
'/login'
|
||||
]
|
||||
|
||||
module.exports = function (options) {
|
||||
const { authDB, jsonResponse } = options
|
||||
const { authDB, jsonResponse, exceptions } = options
|
||||
|
||||
const renderLogin = (res) => {
|
||||
if (jsonResponse) {
|
||||
|
@ -54,7 +49,7 @@ module.exports = function (options) {
|
|||
|
||||
logger.DebugLog(`ID #${user.id}: ${req.url}`, 'auth', 1)
|
||||
|
||||
// TODO: add stat to acesses table
|
||||
UpdateAccess(authDB, user, ip, sessionID)
|
||||
|
||||
dbtools.Update(authDB, 'sessions', {
|
||||
lastAccess: new Date().toString()
|
||||
|
@ -73,10 +68,25 @@ module.exports = function (options) {
|
|||
}
|
||||
}
|
||||
|
||||
function UpdateAccess (db, user, ip, sessionID) {
|
||||
const accesses = dbtools.Select(db, 'accesses', {
|
||||
userId: user.id,
|
||||
ip: ip
|
||||
})
|
||||
|
||||
if (accesses.length === 0) {
|
||||
dbtools.Insert(db, 'accesses', {
|
||||
userID: user.id,
|
||||
ip: ip,
|
||||
sessionID: sessionID,
|
||||
date: new Date().toString()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function GetUserBySessionID (db, sessionID, req) {
|
||||
logger.DebugLog(`Getting user from db`, 'auth', 2)
|
||||
|
||||
// TODO: check same ip?
|
||||
const session = dbtools.Select(db, 'sessions', {
|
||||
id: sessionID
|
||||
})[0]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue