feat(server/middleware): add middleware to track ip addresses on each logged-in request
This commit is contained in:
parent
e8bd88e58c
commit
bd9e3d5336
20
server/middleware/06.ip.ts
Normal file
20
server/middleware/06.ip.ts
Normal file
@ -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();
|
||||
}
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user