Audit changes
This commit is contained in:
@@ -1,16 +1,7 @@
|
||||
/**
|
||||
* Audit Controller
|
||||
* Handles HTTP requests for audit log operations
|
||||
*/
|
||||
|
||||
import { Response } from 'express';
|
||||
import { AuthenticatedRequest } from '../types';
|
||||
import * as auditService from '../services/audit.service';
|
||||
|
||||
/**
|
||||
* GET /audit-logs
|
||||
* Get audit logs with filters and pagination (admin only)
|
||||
*/
|
||||
export async function getAuditLogs(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
@@ -68,10 +59,6 @@ export async function getAuditLogs(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/:id
|
||||
* Get a single audit log by ID (admin only)
|
||||
*/
|
||||
export async function getAuditLogById(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
@@ -103,10 +90,6 @@ export async function getAuditLogById(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/record/:tableName/:recordId
|
||||
* Get audit logs for a specific record (admin only)
|
||||
*/
|
||||
export async function getAuditLogsForRecord(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
@@ -130,10 +113,6 @@ export async function getAuditLogsForRecord(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/statistics
|
||||
* Get audit statistics (admin only)
|
||||
*/
|
||||
export async function getAuditStatistics(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
@@ -158,10 +137,6 @@ export async function getAuditStatistics(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /audit-logs/my-activity
|
||||
* Get current user's own audit logs
|
||||
*/
|
||||
export async function getMyActivity(
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Request, Response } from 'express';
|
||||
import { AuthenticatedRequest } from '../middleware/auth.middleware';
|
||||
import * as authService from '../services/auth.service';
|
||||
import { LoginInput, RefreshInput } from '../validators/auth.validator';
|
||||
import { createAuditLog, getIpAddress, getUserAgent } from '../services/audit.service';
|
||||
|
||||
/**
|
||||
* POST /auth/login
|
||||
@@ -14,6 +15,19 @@ export async function login(req: Request, res: Response): Promise<void> {
|
||||
|
||||
const result = await authService.login(email, password);
|
||||
|
||||
createAuditLog({
|
||||
userId: result.user.id,
|
||||
userEmail: result.user.email,
|
||||
userName: result.user.name,
|
||||
action: 'LOGIN',
|
||||
tableName: 'users',
|
||||
recordId: result.user.id,
|
||||
description: `User logged in successfully`,
|
||||
ipAddress: getIpAddress(req),
|
||||
userAgent: getUserAgent(req),
|
||||
success: true,
|
||||
}).catch(err => console.error('Failed to log login:', err));
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {
|
||||
@@ -24,6 +38,22 @@ export async function login(req: Request, res: Response): Promise<void> {
|
||||
});
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Login failed';
|
||||
const { email } = req.body as LoginInput;
|
||||
|
||||
if (email) {
|
||||
createAuditLog({
|
||||
userId: email,
|
||||
userEmail: email,
|
||||
userName: email,
|
||||
action: 'LOGIN',
|
||||
tableName: 'users',
|
||||
description: `Failed login attempt`,
|
||||
ipAddress: getIpAddress(req),
|
||||
userAgent: getUserAgent(req),
|
||||
success: false,
|
||||
errorMessage: message,
|
||||
}).catch(err => console.error('Failed to log failed login:', err));
|
||||
}
|
||||
|
||||
// Use 401 for authentication failures
|
||||
if (message === 'Invalid email or password') {
|
||||
@@ -89,6 +119,21 @@ export async function logout(req: AuthenticatedRequest, res: Response): Promise<
|
||||
await authService.logout(userId, refreshToken);
|
||||
}
|
||||
|
||||
if (req.user) {
|
||||
createAuditLog({
|
||||
userId: req.user.id,
|
||||
userEmail: req.user.email,
|
||||
userName: req.user.role || req.user.email,
|
||||
action: 'LOGOUT',
|
||||
tableName: 'users',
|
||||
recordId: req.user.id,
|
||||
description: `User logged out`,
|
||||
ipAddress: getIpAddress(req),
|
||||
userAgent: getUserAgent(req),
|
||||
success: true,
|
||||
}).catch(err => console.error('Failed to log logout:', err));
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Logout successful',
|
||||
|
||||
Reference in New Issue
Block a user