@dax-crafta/auth 1.2.9 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dax-crafta/auth",
3
- "version": "1.2.9",
3
+ "version": "1.3.0",
4
4
  "description": "Authentication plugin for crafta framework",
5
5
  "main": "src/index.js",
6
6
  "private": false,
@@ -0,0 +1,55 @@
1
+ const AuditLog = require('../models/audit-log');
2
+ const winston = require('winston');
3
+
4
+ class AuditService {
5
+ constructor() {
6
+ this.logger = winston.createLogger({
7
+ level: 'info',
8
+ format: winston.format.json(),
9
+ transports: [
10
+ new winston.transports.File({ filename: 'audit.log' })
11
+ ]
12
+ });
13
+ }
14
+
15
+ async logActivity(data) {
16
+ const log = new AuditLog({
17
+ userId: data.userId,
18
+ action: data.action,
19
+ ipAddress: data.ipAddress,
20
+ userAgent: data.userAgent,
21
+ details: data.details,
22
+ status: data.status
23
+ });
24
+
25
+ await log.save();
26
+ this.logger.info('Activity logged', { logId: log._id, ...data });
27
+ return log;
28
+ }
29
+
30
+ async getUserActivity(userId, filters = {}) {
31
+ const query = { userId, ...filters };
32
+ return AuditLog.find(query).sort({ timestamp: -1 });
33
+ }
34
+
35
+ async getActivityByDateRange(startDate, endDate) {
36
+ return AuditLog.find({
37
+ timestamp: {
38
+ $gte: startDate,
39
+ $lte: endDate
40
+ }
41
+ }).sort({ timestamp: -1 });
42
+ }
43
+
44
+ async getFailedLoginAttempts(userId, timeWindow) {
45
+ const since = new Date(Date.now() - timeWindow);
46
+ return AuditLog.countDocuments({
47
+ userId,
48
+ action: 'login',
49
+ status: 'failure',
50
+ timestamp: { $gte: since }
51
+ });
52
+ }
53
+ }
54
+
55
+ module.exports = AuditService;