From bd9e3d5336908baa4fb2773b20f725c415c65860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Wed, 11 Oct 2023 13:24:44 -0400 Subject: [PATCH] feat(server/middleware): add middleware to track ip addresses on each logged-in request --- server/middleware/06.ip.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 server/middleware/06.ip.ts diff --git a/server/middleware/06.ip.ts b/server/middleware/06.ip.ts new file mode 100644 index 0000000..04441e3 --- /dev/null +++ b/server/middleware/06.ip.ts @@ -0,0 +1,20 @@ +export default eventHandler(async (ev) => { + if (ev.context.currentUser) { + let log = ev.context.currentUser.ipLog; + if ( + ev.context.clientAddress !== undefined && + !/127\.0\.0\.1|localhost|::1/.test(ev.context.clientAddress) + ) { + let found = log.findIndex((a) => a.ip === ev.context.clientAddress); + if (found !== -1) { + ev.context.currentUser.ipLog[found].lastAccess = new Date(); + } else { + ev.context.currentUser.ipLog.push({ + ip: ev.context.clientAddress!, + lastAccess: new Date(), + }); + } + } + await ev.context.currentUser.save(); + } +});